And the GOOD!>>>HELP ME


#1

n_s = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
for (var i in n_s){
if (i % 3 === 0){
console.log("Fizz");
}else if(i % 5 === 0){
console.log("Buzz");
}else if(i % 5 === 0 && i % 3 === 0){
console.log("FizzBuzz");
}else{
console.log(i);
}
}

The Error is: Oops, try again. You printed Fizz when you should have printed 1


#2

Consider what we have above. A consecutive unit sequence. As a data structure it takes up memory. When we can already emulate the values in the sequence without actually having it as a referable object it makes sense to emulate, rather than take up memory.

But let's say we wish to modify the list to include 'Fizz', 'Buzz', and 'FizzBuzz'. This list would make perfect sense and would be an initialized object. Now we only need to go through the list and change the elements to correspond to the requirements of divisibility.

We should touch on the use of for in object as it relates to arrays. It can produce unexpected results and does not pay heed to the fact that in is an arbitrary key scoper, which makes array indices, "0", "1", "2", .... It makes it under the radar (due to JS type conversion) but skirts the rule of using integer indices with arrays. And there are still other consequences that need not be discussed at present, but do follow up on this general concern.

In other words, the best way to iterate an array is with an index iterator.

for (var i = 0; i < array.length; i++) {}

We have an array of natural numbers in sequence, so now we just need to iterate the sequence and assign or not assign accordingly one of the three string values. When we're done, our list will be created and ready to publish.

That's simple enough. Now let's get on to the logic of how the string values are assigned. We know that there is a condition with divisibility by both 3 and 5, and since we won't be able to test this condition after 3 or 5 have been tested, we will never see FizzBuzz. That means testing the LCM of both, first.

So the stage is set. We have an initialized array, an order of assessment, and all that's left to do is code it.

var sequence = new Array(20);
var i = 0;
for (i; i < sequence.length; i++) { sequence[i] = i + 1; }
for (i--; i >= 0; i--) {
    if (sequence[i] % 15 === 0) { sequence[i] = "FizzBuzz"; }
    if (sequence[i] % 5 === 0) { sequence[i] = "Buzz"; }
    if (sequence[i] % 3 === 0) { sequence[i] = "Fizz"; }
}
for (i++; i < sequence.length; i++) {
    console.log(sequence[i]);
}

There are lots of ways to cook an egg. One hundred, in fact, as I understand. Logic is like that. It can be approached from several angles, just like math. Once we choose an approach we may be locked into it. The above shows how i is locked into the overal execution.

What isn't obvious is that sequence[i] isn't what it was, if any of the above condtions is true. The element is overwritten so the next conditional does not see a number. NaN converts to false.


#3

That means testing the LCM of both, first.

Which in your case, means moving this conditional to the front of the line and testing 3 and 5 after this check.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.