Introduction to Objects 1 (Q3)


#1


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


Oops, try again. You printed Fizz when you should have printed FizzBuzz


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


#2

It is my first time to ask questions here !!!

Please help me solve the problem, Thanks

I find the program correct !!!


#3

Here's the problem, let's say you have i as the number 15. It is divisible by 3 and 5 which should print out "FizzBuzz". However, with your function, it will become "Fizz" as it is divisible by 3 hence that first condition is true and your function ends without it actually reaching the condition to check if it is divisible by 3 and 5.

I suggest moving the if statement that validates if the number is divisible by 3 AND 5 to be the first one to check.


#4

Thanks a lot !!!

The question requires me to have i%3 and i%5 at the same time for the number of 15.

But, the function checks i%3 correct and stops check i%5 so the result is not "FrzzBuzz".

Thank you !!!


#5

After I changed the program to below :
The problem still exists :frowning:

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


#6

Well...the instructions said that if the number is divisible by 3 and 5, print out "FizzBuzz". You are saying to print out "Fizz". You need to switch your print statements too.


#7

Well !!! I solved the problem ^_^

Thanks :slight_smile:


#8

I have another question about For Loop.

I have no idea how to decide when to stop the Loop. Thanks

In my mind, for(var i = 0 ; i < 21 ; i++)
The loop starts at 0 ; The loop ends when i is less than 21

For the following program, the loop ends when it reaches the length of the text.
But, if text.length has 20 characters, will the loop ends at the 19th characters ?

Why the following For Loop will end at text.length ?

for(var i=0 ; i < text.length ; i++){
if(text[i] === "J"){
for(j = i ; j < i + myName.length ; j++){
hits.push(text[j]);
}
}
}


#9

i starts at 0. Your index starts at 0. So your 20th character in your 20 character text is actually the 19th in the sense of javascript index.

So you're loop ends on the 20th character aka the character at index 19.


#10

Hi,

Here's the solution for this exercise.

var number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

for (var i = 0; i < number.length; i++) {
if(number[i] % 3 === 0 && number[i] % 5 === 0) {
console.log("FizzBuzz");
} else if(number[i] % 5 === 0) {
console.log("Buzz");
} else if(number[i] % 3 === 0) {
console.log("Fizz");
} else {
console.log(number[i]);
}
}

Thanks!


#11

Okay !!!

Text.length has 20 characters in fact.

But, in index, the first character is


#12

I'm confused why we have to compare number[I] with 0.

if(number[i] % 3 === 0 && number[i] % 5 === 0)

Like the above.

Please advise,

Darryl


#13

The code that @neoblar shared is used for a certain list called number. But you don't need to do that. You can just use the property of the for loop where you tell it when to stop.

Now, we are not comparing the number[i] to 0 but rather whether it is divisible by 3 and 5. The % gives you the remainder. In order for a number to be divisible by another, the remainder has to be 0, right? That's what we are validating here.


#14

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