-2

This is my first question so sorry if it's dumb. I'm trying to do the classic FizzBuzz exercise in the console using javascrypt, putting the changes into an array for 1 to 100. So far:

let nums = [];

for (let i = 1; i <= 100; i++) {
    nums.push(i);
}

for (num of nums) {
    if (nums[num] % 5 === 0) {
        nums[num] = "Buzz";
        console.log("Buzz");
    }
    else if (nums[num] % 3 === 0) {
        nums[num] = "Fizz";
        console.log("Fizz");
    }
    else if (nums[num] % 3 === 0 && nums[num] % 5 === 0) {
        nums[num] = "FizzBuzz";
        console.log("FizzBuzz");
    }
    else {
        console.log(nums[num]);
    }

}

I'm expecting each FizzBuzz to be put into the array instead of the number, and all the array to be printed on the console. But for some specific numbers it doesn't work. When two vales need to be changed consequently, the second one gives some kind of error. What am i missing?

Thanks for your help!

jabaa
  • 5,844
  • 3
  • 9
  • 30
  • 2
    "*some kind of error*" - can you be more specific? – freedomn-m Nov 03 '22 at 13:38
  • 2
    `nums[num] % 3 === 0 && nums[num] % 5 === 0` should be the first condition, otherwise it's not reachable. If it's true, one of the previous conditions is also always true. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – jabaa Nov 03 '22 at 13:39
  • Start your `.push` at 0 and it works fine (or at least removes the `undefined`). `for let i=0..` – freedomn-m Nov 03 '22 at 13:43
  • `num of nums` should be `num in nums`, otherwise `nums[num]` won't work – jabaa Nov 03 '22 at 13:45
  • @freedomn-m Thanks, that actually solved! But i'm not sure i understand why... – Luis Mendoza Nov 03 '22 at 13:49
  • Added an explanation as an answer. – freedomn-m Nov 03 '22 at 13:49
  • Add some debugging console.log to see, eg `else { console.log(num, nums[num]); }` – freedomn-m Nov 03 '22 at 13:51

2 Answers2

0

The issue is that you're counting starts at 1, and you're using that number as an index. First entry in the array is nums[0] but you're using nums[num] where num==1 so you're getting nums[1] as the first entry.

So first entry is:

num == 1
nums[1] == 2  // as nums[0] == 1

then next

num == 2
nums[2] == 3, so replace with Fizz

now next entry in the array is nums[2] (0,1 above) and nums[2] == Fizz (replaced above)

num == "Fizz"
nums["Fizz"] == undefined
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
0

The loop

for (num of nums) {

iterates over the elements of the array nums and

nums[num]

uses the elements as index for the same array. That doesn't make sense. Instead iterate over the indices with num in nums and use them as indices.

const nums = [];

for (let i = 1; i <= 100; i++) {
    nums.push(i);
}

for (const num in nums) {
    if (nums[num] % 3 === 0 && nums[num] % 5 === 0) {
        nums[num] = "FizzBuzz";
        console.log("FizzBuzz");
    }
    else if (nums[num] % 3 === 0) {
        nums[num] = "Fizz";
        console.log("Fizz");
    }
    else if (nums[num] % 5 === 0) {
        nums[num] = "Buzz";
        console.log("Buzz");
    }
    else {
        console.log(nums[num]);
    }

}

In addition, I changed the order of the conditions. If nums[num] % 3 === 0 && nums[num] % 5 === 0 is true, either nums[num] % 3 === 0 or nums[num] % 5 === 0 is true. That means, the condition nums[num] % 3 === 0 && nums[num] % 5 === 0 is unreachable. You don't have any FizzBuzz in your output.

jabaa
  • 5,844
  • 3
  • 9
  • 30