3 ...And the good!


#1

Funny thing. I used this code:
for (i=1; i<21; i++) {
if (i%3 === 0) {
if (i%5 ===0) {
console.log ("FizzBuzz");
}
console.log ("Fizz");
} else if (i%5 === 0) {
if (i%3 === 0) {
console.log ("FizzBuzz");
}
console.log ("Buzz");
} else {
console.log (i);
}
}

It printed:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Fizz
16
17
Fizz
19
Buzz

It gave me an error: Oops, try again. It looks like you printed out the wrong number of items.

Now, i copied/pasted one of the codes from you guys and it worked.
Any ideas MY code did not work?
IF (nobodyAsnwers===true) {console.log"I understand";}


#2

You printed the wrong number of items, if you look at where fifteen should be it has fizz right after that, and so it goes
FizzBuzz (right-15)
Fizz (wrong-16)
16(right-16)
You need to add a else to them to print Fizz, or Buzz only if it is not a modulo of 3 or 5.


#3

Use
if(i%3 === 0 && i%5 === 0)


#4

I'm not understanding this , i have tried a lot but still can't do it , can someone give me an example ? with the code for me to understand what am I doing wrong?


#5

@itsplaza,

Javascript has the so-called modulo / remainder-operator if used it will return the rest-value.

9%3 You read it as nine modulo three is zero, as 9 divided by 3 has NO rest-value.
22%6 You read 22 modulo six is 4, as 22 divided by 6 will leave you with rest-value 4
16%8 You read it as sixteen modulo eight is zero, as sixteen divided by 8 has NO rest-value.

In this section you have to figure out
(i%3 === 0 && i%5 === 0) to display FizzBuzz // if (imodulo 3 equals zero AND imodulo 5 equals zero)
(i%3 === 0) to display Fizz // else if (i modulo 3 equals zero )
(i%5 === 0) to display Buzz // else if (i modulo 5 equals zero )
AND
if none of the above display i //else display i

You will have to use an IF ELSE-IF ELSE-IF ELSE-statement
within a for-loop.

google search
== the Book ==
javascript remainder operator site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

== discussions / opinions ==
javascript remainder operator site:stackoverflow.com
= http://stackoverflow.com/questions/4467539/javascript-modulo-not-behaving
javascript array site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html


#6

Thanks a lot , this solved my problem immediatly! You made my day and hope you succeed in your life...