forked from JoinCODED/TASK-JS-Functions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge2.js
More file actions
63 lines (61 loc) · 1.05 KB
/
challenge2.js
File metadata and controls
63 lines (61 loc) · 1.05 KB
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
/**
* greet(name):
* - receives a name
* - logs "Hello <name>"
*
* e.g.
* greet("Hamza") logs "Hello Hamza"
*/
function greet(name) {
console.log(`hello ${name}`);
}
greet("wahab");
/**
* isOdd(n):
* - receives a number n
* - returns true if it's odd, false otherwise
*
* e.g.
* isOdd(7) -> true
* isOdd(10) -> false
*/
function isOdd(n) {
if (n % 2 == 0) {
return false;
} else {
return true;
}
}
console.log(isOdd(9));
/**
* oddsSmallerThan(n):
* - receives a number n
* - returns the number of ODD numbers smaller than n
*
* e.g.
* oddsSmallerThan(7) -> 3
* oddsSmallerThan(15) -> 7
*/
function oddsSmallerThan(n) {
// Your code here
return Math.floor(n / 2);
}
console.log(oddsSmallerThan(7));
/**
* squareOrDouble(n):
* - receives a number n
* - returns its square if it's odd
* - returns its double if it's even
*
* e.g.
* squareOrDouble(16) -> 32
* squareOrDouble(9) -> 81
*/
function squareOrDouble(n) {
if (n % 2 == 1) {
return n * n;
} else {
return n * 2;
}
}
console.log(squareOrDouble(8));