Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Perform FizzBuzz challenge using ↴
while loop → repeatedly executes a block of code as long as a specified condition evaluates to true.
modulo operator % (remainder operator) → returns the remainder left over when one operand is divided by a second operand.
while loop repeatedly executes a block of code as long as a specified condition evaluates to true.
while (condition) {
// execute code as long as condition is true
}
let x = 0; → counter
while (x < 4) {
console.log(x);
x++;
}
Initialize a counter variable x outside of the loop.
Condition x < 4 is checked before each iteration.
The loop will continue to run as long as x is less than 4
The loop repeatedly executes a block of code 4 times, from 0 to 3
For each iteration of the loop, the current value of x is printed to the console.
After each iteration, x is incremented by 1 x++
When x reaches 4 the condition evaluates to false, terminating the loop.
0
1
2
3 → printed to console
Modulo operator % (remainder operator) returns the remainder left over when one number is divided by a second number.
10 % 2 → remainder 0
10 % 3 → remainder 1
10 % 4 → remainder 2
10 % 5 → remainder 0
10 % 6 → remainder 4
10 % 7 → remainder 3
10 % 8 → remainder 2
10 % 9 → remainder 1
10 % 10 → remainder 0
10 % 2 === 0 → 10 is divisible by 2
10 % 5 === 0 → 10 is divisible by 5
10 % 10 === 0 → 10 is divisible by 10
Initialize a variable to set the upper limit for the FizzBuzz sequence.
const numLimit = 15; → user input
Define a function fizzBuzz to run the fizzBuzz challenge.
function fizzBuzz(num) {}
The function takes a number as input num and returns the fizzBuzz sequence to console for n iterations.
Initialize a counter variable.
let x = 1 x
Loop from 1 to num (inclusive).
while (x <= num) {}
During each iteration of the loop, conditional statements determine when to append -
"Fizz", "Buzz", "FizzBuzz" or the current number x itself.
Check if x is a multiple of both 3 and 5
if (x % 15 === 0) {}
If true, print "FizzBuzz"
console.log("FizzBuzz")
Check if x is a multiple of 3
else if (x % 3 === 0)
If true, print "Fizz"
console.log("Fizz")
Check if x is a multiple of 5
else if (x % 5 === 0)
If true, print "Buzz"
console.log("Buzz")
If x is not a multiple of 3 or 5 print the number x itself
console.log(x)
Increment the counter for the next iteration of the loop.
x++
Call the function with ↴
fizzBuzz(numLimit);
Perform FizzBuzz for 15 iterations.
const numLimit = 15;
function fizzBuzz(num) {
let x = 1;
while (x <= num) {
if (x % 15 === 0) {
console.log("FizzBuzz");
} else if (x % 3 === 0) {
console.log("Fizz");
} else if (x % 5 === 0) {
console.log("Buzz");
} else {
console.log(x);
}
x++;
}
}
call function
fizzBuzz(numLimit); returns ↴
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz → printed to console
Alternative ↴
(x % 3 === 0 && x % 5 === 0) same as ↴
(x % 15 === 0)