Goal: create a function
fizzBuzz()that returns an array of 100 elements: multiples of 3 are replaced with “Fizz”, multiples of 5 with “Buzz”, and multiples of both 3 and 5 with “FizzBuzz”.
/**
* FizzBuzz function - a classic divisibility test problem.
* Returns an array where numbers divisible by 3 are replaced with "Fizz",
* divisible by 5 with "Buzz", and divisible by both 3 and 5 (i.e., 15) with "FizzBuzz".
* @returns {Array} Result array following FizzBuzz rules
*/
function fizzBuzz() {
// Initialize array to store results
const result = [];
// Iterate through numbers 1 to 100 inclusive
for (let i = 1; i <= 100; i++) {
// Check divisibility by 15 first (to prevent
// triggering separate 3 or 5 conditions)
if (i % 15 === 0) {
result.push('FizzBuzz'); // Divisible by both 3 and 5
}
// If not divisible by 15, check for 3
else if (i % 3 === 0) {
result.push('Fizz'); // Divisible only by 3
}
// If not divisible by 15 or 3, check for 5
else if (i % 5 === 0) {
result.push('Buzz'); // Divisible only by 5
}
// If none of the above conditions are met
else {
result.push(i); // Add the number itself
}
}
// Return the populated results array
return result;
}Why this way:
function fizzBuzz() {
const result = [];
for (let i = 1; i <= 100; i++) {
let str = '';
if (i % 3 === 0) str += 'Fizz';
if (i % 5 === 0) str += 'Buzz';
result.push(str || i);
}
return result;
}Why this way:
function fizzBuzz() {
return Array.from({ length: 100 }, (_, idx) => {
const i = idx + 1;
let str = '';
if (i % 3 === 0) str += 'Fizz';
if (i % 5 === 0) str += 'Buzz';
return str || i;
});
}Why this way:
Write a function fizzBuzz that returns an array of length 100 with replacements:
fizzBuzz()[2]; // "Fizz" (number 3)
fizzBuzz()[4]; // "Buzz" (number 5)
fizzBuzz()[14]; // "FizzBuzz" (number 15)fizzBuzzGoal: create a function
fizzBuzz()that returns an array of 100 elements: multiples of 3 are replaced with “Fizz”, multiples of 5 with “Buzz”, and multiples of both 3 and 5 with “FizzBuzz”.
/**
* FizzBuzz function - a classic divisibility test problem.
* Returns an array where numbers divisible by 3 are replaced with "Fizz",
* divisible by 5 with "Buzz", and divisible by both 3 and 5 (i.e., 15) with "FizzBuzz".
* @returns {Array} Result array following FizzBuzz rules
*/
function fizzBuzz() {
// Initialize array to store results
const result = [];
// Iterate through numbers 1 to 100 inclusive
for (let i = 1; i <= 100; i++) {
// Check divisibility by 15 first (to prevent
// triggering separate 3 or 5 conditions)
if (i % 15 === 0) {
result.push('FizzBuzz'); // Divisible by both 3 and 5
}
// If not divisible by 15, check for 3
else if (i % 3 === 0) {
result.push('Fizz'); // Divisible only by 3
}
// If not divisible by 15 or 3, check for 5
else if (i % 5 === 0) {
result.push('Buzz'); // Divisible only by 5
}
// If none of the above conditions are met
else {
result.push(i); // Add the number itself
}
}
// Return the populated results array
return result;
}Why this way:
function fizzBuzz() {
const result = [];
for (let i = 1; i <= 100; i++) {
let str = '';
if (i % 3 === 0) str += 'Fizz';
if (i % 5 === 0) str += 'Buzz';
result.push(str || i);
}
return result;
}Why this way:
function fizzBuzz() {
return Array.from({ length: 100 }, (_, idx) => {
const i = idx + 1;
let str = '';
if (i % 3 === 0) str += 'Fizz';
if (i % 5 === 0) str += 'Buzz';
return str || i;
});
}Why this way:
Write a function fizzBuzz that returns an array of length 100 with replacements:
fizzBuzz()[2]; // "Fizz" (number 3)
fizzBuzz()[4]; // "Buzz" (number 5)
fizzBuzz()[14]; // "FizzBuzz" (number 15)fizzBuzzThe code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!