-
Notifications
You must be signed in to change notification settings - Fork 0
/
codewars8kyu3.js
97 lines (73 loc) · 2.12 KB
/
codewars8kyu3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//Given an array of integers, return a new array with each value doubled.
function maps(x){
let newX = x.map(item => item * 2);
return newX;
}
/*Every day you rent the car costs $40. If you rent the car for 7 or more days, you get $50 off your total.
Alternatively, if you rent the car for 3 or more days, you get $20 off your total.Write a code that gives out the total amount for different days(d).*/
function rentalCarCost(d) {
let count = 0;
if(d >= 7) {
count = 40*d - 50;
}
else if(3 <= d && d < 7) {
count = 40*d - 20;
}
else {
count = 40*d;
} return count;
}
//Given a year, return the century it is in.
function century(year) {
let count = 1;
if (year <= 100) {
return count;
}
else {
do {
year -= 100;
count += 1;
} while(year > 100);
return count;
}
}
/* Your inputs: a positive integer, n, greater than or equal to one. n is provided, you have NO CONTROL over its value.
Your expected output is an array of positive integers from 1 to n (inclusive). */
function preFizz(n) {
let arr = [];
let count = 0;
do {
n -= 1;
count += 1;
arr.push(count);
} while(n > 0);
return arr;
}
// Create a function that gives a personalized greeting. This function takes two parameters: name and owner.
function greet (name, owner) {
let result = (name === owner) ? "Hello boss" : "Hello guest";
return result;
}
//Implement a function which multiplies two numbers.
function multiply(a, b) {
return a*b;
}
//Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0.
var summation = function (num) {
let result = 0;
for(let i = 0; i <= num; i +=1) {
result += i;
}
return result;
}
//You get an array of numbers, return the sum of all of the positives ones.
function positiveSum(arr) {
let result = 0;
for(let item of arr) {
if(item > 0) result += item;
} return result;
}
//Complete the method that takes a boolean value and return a "Yes" string for true, or a "No" string for false.
function boolToWord( bool ){
return result = (bool === true) ? "Yes" : "No";
}