1

I want to populate an empty array with the classical fizzbuzz game (numbers from 1 to 100, when a number is divisible by 3 print 'Fizz, divisible by 5 print 'Buzz', divisible by both 3 and 5 print 'Fizzbuzz'). The problem is, when I write code like in the first portion of code below saving my array[i] in a more convenient variable my if-else if statement doesn't work, only normal numbers are printed; but when I use array[i] instead of a variable everything works fine, as you can see in the second portion of code, where 'Fizz', 'Buzz', 'FizzBuzz' overwrite the normal numbers. They should be the same thing right?

First portion of code with a variable instead of array[i]

var numberArray = [];
var number = 0

for (var i = 0; i < 100; i++) {
    number += 1;
    thisNumber = numberArray[i];
    numberArray.push(number);

    if (number %3 ==0 && number %5 ==0) {
        thisNumber = 'FizzBuzz';
    } else if ( number %3 ==0 ) {
        thisNumber = 'Fizz';
    } else if ( number %3 ==0 ) {
        thisNumber = 'Buzz';
    }
}

console.log(numberArray);

Second portion of code with array[i] instead of a variable

var numberArray = [];
var number = 0

for (var i = 0; i < 100; i++) {
    number += 1;
    numberArray.push(number);

    if (number %3 ==0 && number %5 ==0) {
        numberArray[i] = 'FizzBuzz';
    } else if ( number %3 ==0 ) {
        numberArray[i] = 'Fizz';
    } else if ( number %3 ==0 ) {
        numberArray[i] = 'Buzz';
    }
}

console.log(numberArray);
Alebacce
  • 63
  • 12

2 Answers2

0

Reassigning a variable, by itself, never has any side effects (except in the most rare situations which aren't worth worrying about). Doing thisNumber = 'FizzBuzz'; does not change anything about how thisNumber may have happened to be used in the past.

Push after assigning to thisNumber. You also want to push thisNumber, not number.

You also need to change the final % 3 to % 5 - you're currently testing % 3 twice.

var numberArray = [];
for (var i = 0; i < 100; i++) {
  let thisNumber = i;
  if (i % 3 == 0 && i % 5 == 0) {
    thisNumber = 'FizzBuzz';
  } else if (i % 3 == 0) {
    thisNumber = 'Fizz';
  } else if (i % 5 == 0) {
    thisNumber = 'Buzz';
  }
  numberArray.push(thisNumber);
}

console.log(numberArray);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I tried with your code but it works with indexes that is not what I want, because I want to work with the content of them. It's due to a my error, since I posted the question at the beginning writing `thisNumber = I` and not `thisNumber =numberArray[i]` as it should be. I ask you sorry, anyway I edited correctly the question – Alebacce Mar 27 '21 at 18:16
  • The code in my answer does not use indicies - please look at it again. The main change in the logic of my answer is that the number is pushed at the *end* of the loop, not at the *start* of the loop. (If you push a number, then reassign the variable that pointed to the number, the array will not be changed.) – CertainPerformance Mar 27 '21 at 19:17
0

In JavaScript a variable is just a reference to an object and an assignment changes where it points to. In

thisNumber = 'FizzBuzz';

you create a new string object and reference it with thisNumber. In

numberArray[i] = 'FizzBuzz';

you modify the i-th element of the array numberArray.

You can't create a reference to an array element and modify it with an assignment. That's not possible in JavaScript.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62