Introduction to Objects I


#1

Print out the numbers from 1 - 20.
The rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
Otherwise, just print out the number.


#2

@petacoder,
Javascript has the so-called Remainder-operator %
is actually a modulo-operator
and 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
10%5 You read 10 nodulo five is zero, as 10 divided by 5 will have 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
the for-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

== 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/survey.html
http://javascript.crockford.com/code.html


#3

what lesson is this for


#4

I'm on this lesson and have written the code below. For some reason this isn't printing "FizzBuzz". Does anyone know what I'm doing wrong?

var numbers = function(){

for (i = 1; i < 21; i++){

    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);
    }

}

};

numbers();


#5

@devblaster44527,

1

You have to put the most-specific condition first

 (i % 5 === 0 && i % 3 === 0){

2

In your FOR-loop definition
please use the var-keyword in your initialization
like

for (var i = 1; i < 21; i++) {

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for


#6

Great, thank you. That's a useful principle to know, putting the most specific condition first. It's working now.

The lessons in the course mentioned that you didn't need to have to delcare a variable in the "for" loop? You can abbreviate it to the name, i.e. here the letter "i". Is it better practice to actually write var?


#7

@devblaster44527,
Have a read
http://stackoverflow.com/questions/18465211/javascript-loop-variable-scope