Introduction to Objects I-...And the good!


#1


https://www.codecademy.com/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661#


I have the entire script written and I think it logs the right stuff, but the message "Oops, try again. It looks like you printed out the wrong number of items." keeps appearing.


for (i = 1; i < 21; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
        console.log ("FizzBuzz");
    }else if (i % 3 === 0) {
        console.log ("Fizz");
    }else if (i % 5 === 0) {
        console.log ("Buzz");
    }
}


#2

You need to check if the number is divisable by 5 before you check if it is divisable by 3.
You also want to console.log the remaining numbers. Add a else statement to the bottom like this.

else {
        console.log(i);
    }

#3

Thank you!
I added the last part to my script and now it works.

it looks like this now

...

for (i = 1; i < 21; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log ("FizzBuzz");
}else if (i % 3 === 0) {
console.log ("Fizz");
}else if (i % 5 === 0) {
console.log ("Buzz");
}else{
console.log(i);
}
}

...


#4

Hey guys i have a question regarding this matter.

I wrote my code exactly the same but changed my order, so for the " if " i started whit the conditional for Buzz > eg. i % 5 === 0, then the else if for the Fizz > eg. i % 3 === 0, and the last else if i stated the condition for FizzBuzz > eg. i%5 === 0 && i % 3 === 0. When i ran the code the condition for the Fizzbuzz did not print. When i shufuled the code for the same as the ryan5853 everthing worked fine.

My question is, i've noticed now that the order matters, but can any of you be more especific why order matters and how do i cheek this order in future problems, because its not very especific in the course.

TYIA


#5

me too :slight_smile:


#6

its because it checks the different parts in order as it goes down. it looks at the first thing, then moves on to the other else if the number does not match. I put the 5 and 3 part first because I knew that they would be triggered by the only 5 or 3 and never make it to the and part with the else.

Basically it kinda cycles, through each number until it finds one that fits.

and also sorry for getting back to you so late


#7

Still a bit confusing, not for this example but for future reference, when i have a 3 part condition, what goes where...

no worries dude :stuck_out_tongue:


#8

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