JavaScript Challenge - FizzBuzz

Your use of fb for the function name and for your internal array though is bad practice. Also your output isn’t entirely correct. It needs to start from 1 not 0. As currently your first entry is “FizzBuzz” due to using mod on 0

The function name is irrelevant and the variable name is in a different scope, so has no conflict. As for starting at zero, that just shows that 'fizzBuzz(0)` is ‘FizzBuzz’. Mathematically that is true.

I know its in a different scope, I said bad practice not actually causing an error. Also I don’t disagree that mathmatically it’s true but per the instructions for this challenge

Write a fizzbuzz() function that takes in a number, n , and returns an array of the numbers from 1 to n . For multiples of three, use "Fizz" instead of the number, and for the multiples of five, use "Buzz" . For numbers that are multiples of both three and five, use "FizzBuzz" (capitalization matters).

It should be from 1 to n

Now you’re nitpicking. I’m not doing the exercise, but exploring options. As for best practice, don’t preach to the choir. Write good code for yourself.

I’ve seen many ‘clever’ solutions to the FizzBuzz challenge over the past few years. Clever isn’t necessarily straightforward or efficient. I’ve found that the most efficient solution for this challenge is much more naive than clever. For example, I ran my very naive solution with an argument of 100,000,000 three times with run times (seconds) of 5.99, 5.295 & 5.39. I also ran your code with the same argument three times with run times of 9.522, 9.048 & 8.955. The naive approach averages 3.6 seconds faster. I’m not trying to rain on your parade. Just pointing out that sometimes the most simple solution is best.

Naive Solution
const fizzBuzz = function(n) {
    fb = [];
    for(let i = 1; i <= n; i++) {
      if(i % 15 === 0) {
        fb.push('FizzBuzz');
      } else if(i % 5 === 0) {
        fb.push('Buzz');
      } else if(i % 3 === 0) {
        fb.push('Fizz');
      } else {
        fb.push(i);
      }
    }
    return fb;
}
5 Likes

Alternative solutions:

Sparse Array with for loop - edited not really sparse array as each element is assigned

function fizzbuzz2(n) {
  let arr = [];
  for (let i = 1; i <= n; i++) {
    let str = "";
    (i % 3) === 0 && (str += "Fizz");
    (i % 5) === 0 && (str += "Buzz");
    arr[i - 1] = (str ? str : i);
  }
  return arr;
}

New array with fill and using map

function fizzbuzz1(n) {
  return new Array(n).fill(n)
    .map((el, i) => {
      let str = "";
      i += 1;
      (i % 3) === 0 && (str += "Fizz");
      (i % 5) === 0 && (str += "Buzz");
      return (str ? str : i);
    });
}

// alternative to my orginal solution, using if statment to check process continuation

function fizzbuzz(n) {
  let arr = [];
  for (let i = 0; i < n; i++) {
    arr[i] = i + 1;
  }
  for (let i = 1; i <= n; i++) {
    let [mod3, mod5] = [i % 3 === 0, i % 5 === 0];
    if (mod3 || mod5) {
      let str = "";
      mod3 && (str += "Fizz");
      mod5 && (str += "Buzz");
      str && (arr[i - 1] = str);
    }
  }
  return arr;
}
1 Like

You say naïve but I actually think your solution is rather clever. Utilizing the fact that mod 15 mathematically works for 3 and 5 then chaining the if statement so it only processes what it needs to.

3 Likes

I used to be of a mind that only checking mod 3 and mod 5 was better, and building a string from '' to 'Fizz' to 'FizzBuzz' by concatenating seemed efficient. Then I realized that computers aren’t humans, and since strings are immutable, and have to be replaced in memory with the new string with each concatenation, checking mod 15 saves the computer significant work.

1 Like

Over the years we’ve encountered many users who never would have thought of using the LCM of 3 and 5 to identify ‘FizzBuzz’. I don’t think it is clever as much as intuitive.

By definition if it was intuitive more people would of thought of it. Therefore the solution was clever using a math fact that other people hadn’t considered, not intuitive.

my version:

function fizzbuzz(n) {
// Write your code here
let numbers = ;
for (let i=1; i<=n; i++){
let num;
let byThree = i%3 === 0;
let byFive = i%5 ===0;
if (byThree && byFive){
num = “FizzBuzz”;
} else if (byThree){
num = “Fizz”;
} else if (byFive){
num = “Buzz”;
} else {
num = i;
}
numbers.push(num);
}
return numbers;
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;

function fizzbuzz(n) {
// Write your code here
const numArray =

for(let i=1; i<=n; i++){
numArray.push(i)
}

for(let j=0; j < numArray.length; j++){
let currentNum = numArray[j]
if(currentNum % 3 === 0 && currentNum % 5 === 0){
numArray.splice(j,1,“FizzBuzz”)
}else if(currentNum % 3 === 0){
numArray.splice(j,1,“Fizz”)
}else if(currentNum % 5 === 0){
numArray.splice(j,1,“Buzz”)
}
}
return numArray
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;

Perfomance test of 6 different approaches from answers above (with some variations) show than next one is the fastest (but not too much):

function fizzbuzz(n) {
  let arr = [];
  for (let i = 1; i <= n; i++) {
    let fizz = i % 3 === 0;
    let buzz = i % 5 === 0;
    let result = i;
    if (fizz && buzz) {
      result = "FizzBuzz";
    } else if (fizz) {
      result = "Fizz";
    } else if (buzz) {
      result = "Buzz";
    }
    arr.push(result);
  }
  return arr;
}

Test parameters: n=10000, 3000 calls, 20 passes

This is my first challenge attempt!
A bit basic but it got the job done:

function fizzbuzz(n) {
  // Write your code here
  let answerArray = [];
  for(let i=1; i<=n; i++){
    if(i%3===0 && i%5!==0)
      answerArray.push('Fizz');
    else if(i%5===0 && i%3!==0)
      answerArray.push('Buzz');
    else if(i%3===0 && i%5===0)
      answerArray.push('FizzBuzz')
    else
      answerArray.push(i);
  }
  return answerArray;
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
  let arr = []
  for(let i = 1; i <= n; i++) {
    arr.push(
      i % 3 === 0 && i % 5 === 0 ? "FizzBuzz" : 
      i % 3 === 0 ? "Fizz" : 
      i % 5 === 0 ? "Buzz" : i )
  }
  return arr
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
  // Write your code here
  let ans =[];

  for(let i=1; i<=n; i++){
    if(i%15==0){
      ans.push('FizzBuzz');
    }else if(i%3==0){
      ans.push('Fizz');
    }else if(i%5==0){
      ans.push('Buzz');
    }else{
      ans.push(i);
    }
  }
  return ans;
}

console.log( fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
  // Write your code here
  let array = [];
  for (let i = 1; i <= n; i++) {
    if (i % 3 === 0 && i % 5 === 0) array.push('FizzBuzz');
    else if (i % 3 === 0) array.push('Fizz');
    else if (i % 5 === 0) array.push('Buzz');
    else array.push(i);
  }
  return array;
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
  // Write your code here
  let fizzbuzz = ['Fizz','Buzz','FizzBuzz'];
  const checkNumber = (_, idx) => {
    idx++
    let i = Math.max(0, 1 - idx % 3) + Math.max(0, 1 - idx % 5)*2 - 1;
    return fizzbuzz[i] || idx;
  }
  return new Array(n).fill(0).map(checkNumber);
}

console.log(fizzbuzz(16));
1 Like
function fizzbuzz(n) {

  let fizzBuzzArr = []

  for (let i = 1; i <= n; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
      fizzBuzzArr.push('FizzBuzz')
    } else if (i % 5 == 0 ) {
      fizzBuzzArr.push('Buzz')
    } else if (i % 3 == 0) {
      fizzBuzzArr.push('Fizz')
    } else {
      fizzBuzzArr.push(i)
    }
  }

  return fizzBuzzArr;

}

function fizzbuzz(n) {
// Write your code here
let arr = ;
for(let i = 1; i <= n; i++){
let x = i
if(x % 15 === 0){
x = “FizzBuzz”;

}
else if(x % 5 === 0){
  x = "Buzz";

}
else if(x % 3 === 0){
  x = "Fizz";
 
}
else {
  x = x;
}
arr.push(x);

}
return arr
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;

My fizz buzz code