Any clue what is missing in my code?

[FizzBuzz Challenge] https://www.codecademy.com/code-challenges/code-challenge-fizzbuzz-javascript

Currently I am working on this challenge which I can’t quite figure out what is missing in my code. The challenge is to create a fizzbuzz() function that takes in a number, n, and return an array of the numbers from 1 to n. For multiples of 3, use “Fizz” instead of the number, and for multiples of 5, use “Buzz”. For numbers that are multiples of both 3 and 5, use “FizzBuzz”.

For example, fizzbuzz(16) should return [1, 2, “Fizz”, 4, “Buzz”, “Fizz”, 7, 8, “Fizz”, “Buzz”, 11, “Fizz”, 13, 14, “FizzBuzz”, 16].

Here is my code:

function fizzbuzz(n) {

// Write your code here

let arr = [];

for (i = 1; i <= n; i++) {

arr[i] = i;

if (i % 3 === 0 && i % 5 === 0) {

  arr[i] = "FizzBuzz";

} else if (i % 3 === 0) {

  arr[i] = "Fizz";

} else if (i % 5 === 0) {

  arr[i] = "Buzz";

} else {

  arr[i] = i;

}

}

arr.shift();

console.log(arr);

}

fizzbuzz(16);

// Leave this line for testing:

module.exports = fizzbuzz;

When I run the code, the test doesn’t seem to be completed even though the output produced the same with what it asked for. Any clue why this happen?

First of all, you should declare the i variable as a let inside the for loop.

Then, I would personally suggest you use the push method that arrays use.

Finally, with functions you need to return the array instead of logging it to the console.

I hope I guided you to a better solution :slight_smile:

1 Like

Thank you jjeand :+1: . Test test is completed. You rock! :love_you_gesture:

1 Like