1

I am trying to figure out what I am doing wrong with this problem. I seem to be missing something that is not allowing my code to work. I need to use a function to create an array that takes one number n, and that loops through an array from the numbers 1 - 16, while also replacing all the numbers divisible by 3 with the string 'fizz', and all divisible by 5 with the word 'buzz', and any number that is divisible by both must be replaced by the string 'fizzbuzz'.

I have gone over this several times but for some reason keep coming up with just an empty array for my result when it is logged to the console. I would greatly appreciate it if someone could give me some tips as to why my code isn't working so that I can understand the concept easier. Here is my code:

const results = [];

const fizzbuzz = (n) => {
  var results = []
  for (let n = 1; results.length < 16; n++) {
    if (n % 3 === 0 && n % 5 === 0) {
      results.push('FizzBuzz')
    } else if (n % 3 === 0) {
      results.push('fizz');
    } else if (n % 5 === 0) {
      results.push('buzz')
    } else {
      results.push(n)
    }
  }
  return results
};

fizzbuzz(16);
console.log(results);

This is what it is supposed to come out to:

[1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16]

But this is what I keep getting as an answer:

[]
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
Tanya
  • 25
  • 3
  • 2
    Assign the results of calling the function to results - `const results = fizzbuzz(16); console.log(results);` – Ori Drori Jan 10 '20 at 18:28
  • 3
    Because `var results = []`..... is not `const results = [];` Your `fizzbuzz` returns something and you do not use it. – epascarello Jan 10 '20 at 18:28
  • 3
    The `results` variable that you're creating and manipulating *inside* of your `fizzbuzz` function has no association to the `results` variable *outside* the function. – Tyler Roper Jan 10 '20 at 18:29

5 Answers5

1

Your fizzbuzz function returns results, so just assign that to a variable and log it:

var results = fizzbuzz(16);
console.log(results);
ekneiling
  • 159
  • 1
  • 8
0

Your first issue is that the results variable declared outside your function and the results variable declared inside your function are totally different variables. You only need to declare the variable in the function, then you can make a new variable to hold the function result:

const res = fizzbuzz(16);
console.log(res);

or even simpler:

console.log(fizzbuzz(16));

The second issue is in your for loop, you are reassigning n when you should be making a new variable (most commonly called i). You also use a static number (16) as your limit in the expression results.length < 16, when you should use the variable n:

  for (let i = 0; results.length < n; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
      results.push('FizzBuzz')
    } else if (i % 3 === 0) {
      results.push('fizz');
    } else if (i % 5 === 0) {
      results.push('buzz')
    } else {
      results.push(i)
    }
  }

Overall you are taking the correct approach, but you should review variables and for loops as you are using both a bit incorrectly.

Robbie Milejczak
  • 5,664
  • 3
  • 32
  • 65
0

You are playing with another results inside the function, and outside the function result will remain empty. If you remove the variable which is inside the function now every manipulation will be on the same variable.

const results = [];

const fizzbuzz = (num) => {

  for(let n=1; n<num; n++){
    if(n%3 === 0 && n % 5 === 0){
     results.push('FizzBuzz')
    } 
    else if(n % 3 === 0){
     results.push('fizz'); 
    } 
    else if(n % 5 === 0){
      results.push('buzz')
    }
    else { 
      results.push(n)
    }
  }

};

fizzbuzz(16);
console.log(results);
Addis
  • 2,480
  • 2
  • 13
  • 21
0

There are two glaring issues with your code.

  1. You define the variable results twice, one as a constant and one as var.

If you remove the var results = [] definition, your code will work, as it's able to store the results through closures. Basically, the scope of your const results encapsulates the scope of your function, so your function will have access to const results = []

  1. Your function isn't returning anything. This wouldn't be a problem if you were attempting to store the results via closures, but looking at your code, that wasn't your intention. What you should've done is store the results in a variable, as

    const results = fizzbuzz(16)

alex067
  • 3,159
  • 1
  • 11
  • 17
0

This is my FizzBuzz implementation that uses Array.from() to generate an array with the fizz/buzz/number according to the rules:

const fizzbuzz = length => Array.from({ length }, (_, i) => {
  if (i % 3 === 0 && i % 5 === 0) return 'FizzBuzz';
  
  if (i % 3 === 0) return 'fizz';
  
  if (i % 5 === 0) return 'buzz';
  
  return i;
})

const results = fizzbuzz(16); // assign the return value of the function to results
console.log(results);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209