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 ↴
for loop → executes a block of code a number of times.
modulo operator % (remainder operator) → returns the remainder left over when one operand is divided by a second operand.
for loop repeatedly executes a block of code until a specified condition evaluates to false.
The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.
for (let x = 0; x < 4; x++) {
console.log(x);
}
Loop variable x is initialized to 0
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 variables to hold user inputs ↴
const numLimit = 12; → number of iterations.
const firstNum = 4; → define the first divisor.
const secondNum = 5; → define the second divisor.
const firstStr = "Hello"; → define the string to replace multiples of the first divisor.
const secondStr = "World"; → define the string to replace multiples of the second divisor.
Define a function fizzBuzz to run the fizzBuzz challenge.
function fizzBuzz(num, num1, num2, str1, str2) {}
The function takes numLimit as input num
The function takes firstNum as input, num1
The function takes SecondNum as input, num2
The function takes firstStr as input, str1
The function takes secondStr as input, str2
The function returns the fizzBuzz sequence to console for n iterations.
Initialize a variable to hold the output message.
let output output
for loop repeatedly executes a block of code until a specified condition evaluates to false.
Loop through numbers from 1 to num
for (let x = 1; x <= num; x++) {}
During each iteration of the loop, conditional statements determine when to assign -
"Hello", "World", or the current number x itself.
Check if x is a multiple of 4 num1
if (x % num1 === 0) output += str1
If true, append "Hello" str1 to output string.
Check if x is a multiple of 6 num2
if (x % num2 === 0) output += str2
If true, append "World" str2 to output string.
If x is a multiple of both 4 num1 and 6 num2
"Hello" and "World" are both appended to "HelloWorld"
If output is still empty ""
x is neither a multiple of 4 num1 nor 6 num2
Assign the current number x to output string.
if (output === "") output = x
Print the output for the current number.
console.log(output)
Call the function with ↴
fizzBuzz(numLimit, firstNum, secondNum, firstStr, secondStr);
Perform FizzBuzz with user inputs for 12 iterations.
const numLimit = 12;
const firstNum = 4;
const secondNum = 6;
const firstStr = "Hello";
const secondStr = "World";
function fizzBuzz(num, num1, num2, str1, str2) {
for (let x = 1; x <= num; x++) {
let output = "";
if (x % num1 === 0) output += str1;
if (x % num2 === 0) output += str2;
if (output === "") output = x;
console.log(output);
}
}
call function
fizzBuzz(numLimit, firstNum, secondNum, firstStr, secondStr);
returns ↴
1
2
3
Hello
5
World
7
Hello
9
10
11
HelloWorld → printed to console
Alternative using if/else statements ↴
function fizzBuzz(num, num1, num2, str1, str2) {
for (let x = 1; x <= num; x++) {
let output = "";
if (x % num1 === 0 && x % num2 === 0) {
output = str1 + str2;
} else if (x % num1 === 0) {
output = str1;
} else if (x % num2 === 0) {
output = str2;
} else {
output = x;
}
console.log(output);
}
}