-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolFlow.js
More file actions
74 lines (50 loc) · 1.33 KB
/
controlFlow.js
File metadata and controls
74 lines (50 loc) · 1.33 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
64
65
66
67
68
69
70
71
72
73
// Made this just cause I am not going to procrastinate on control flow topic
// if else statement
// switch case statement
// for loop
// while loop
// do-while loop
// infinite loop
// for-in loop (to interate in an object)
const book = {
name: "How to win friends",
cost: 150,
author: "classified"
};
for (const key in book) {
console.log(book[key]);
}
// for-of loop (to interate in an array)
let numbers = [2,3,4,5,6];
for (const number of numbers) {
console.log(number);
}
// break and continue
// implementing a function that accepts two number and returns the maximum one
function maximum(num1, num2) {
return num1 >= num2 ? num1 : num2;
}
// program to display even and odd numbers of array
let numberArr = [1,2,3,4,5,6];
function displayEvenNumbers(number) {
for (const number of numberArr) {
if (number % 2 == 0) console.log(number);
}
}
function displayOddNumbers(number) {
for (const number of numberArr) {
if (number % 2 != 0) console.log(number);
}
}
// implementing fizzBuzz function
function fizzBuzz(number) {
if (typeof number != 'number') return number;
if (number % 15 == 0)
return "fizzBuzz";
else if (number % 3 == 0)
return "fizz";
else if (number % 5 == 0)
return "buzz";
else
return number;
}