-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem-4.js
More file actions
41 lines (37 loc) · 734 Bytes
/
problem-4.js
File metadata and controls
41 lines (37 loc) · 734 Bytes
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
// FizzBuzz Problem
// Write a function that prints numbers from 1 to n. For multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz", and for multiples of both, print "FizzBuzz".
// Example: fizzBuzz(15) should print:
function fizzBuzz(n) {
for (let i = 1; i <= n; i++) {
if ((i % 3 === 0) && (i % 5 === 0)) {
console.log("FizzBuzz")
}
else if (i % 3 === 0) {
console.log("Fizz")
}
else if (i % 5 === 0) {
console.log("Buzz")
}
else {
console.log(i);
}
}
}
fizzBuzz(15);
/**
* 1
* 2
* Fizz
* 4
* Buzz
* Fizz
* 7
* 8
* Fizz
* Buzz
* 11
* Fizz
* 13
* 14
* FizzBuzz
*/