-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathwarmUp5.js
61 lines (55 loc) · 1.69 KB
/
warmUp5.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
// 1) Write a function named greaterNum that:
// - takes 2 arguments, both numbers.
// - returns whichever number is the greater (higher) number.
// ex greaterNum(5, 10) => "The greater number of 5 and 10 is 10."
function greaterNum(num1,num2){
if ( num1 > num2) {
return 'The greater number of ' + num1 +' and '+num2 +' is '+num1 ;
}
return 'The greater number of ' + num1 +' and '+num2 +' is '+num2 ;
}
// 2) Write a function named isEven using for loop that
// - iterate from x to y.
// - return array contain the even values,
// ex: isEven(1,10) => [2,4,6,8,10]
function isEven(x, y) {
for (var i=0 ;i<y ; i++)
if()
// }
//3) write a function named sum that
// - Use a while loop to add up the numbers from x to y.
// ex sum(1,5) => 15
function sum(x,y) {
var total = 0;
var i=1
while (i <= y) {
total = total + i;
i++;
}
return total;
}
//4) Write a function named factorial that
// - Use Recursion to calculate the factorial of a number
// - the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n
// - 5! = 5*4*3*2*1 = 120
// ex : factorial(5) => 120
function factorial(n) {
var total = 1;
var i=0
while (i <= n) {
total = total * i;
i++;
}
return total;
}
//5) write a function named decimals
//- the function will format a number up to specified decimal places
//- the function will return a string
//- if the parameters not a number return false
// ex :
// decimals(2100, 'a') ==> false
// decimals('a', 5) ==> false
// decimals(2.100212, 2) ==> '2.10'
// decimals(2.100212, 3) ==> '2.100'
// decimals(2100, 2) ==> '2100.00'
//