Wednesday, May 20, 2015

FizzBuzz Part 3

Today i'm going over using a for loop in javascript to do a countdown from 10 - 0 and printing "Blast Off!" when the counter gets to zero.

for ( i = 10; i >=0 ; i-- ) {
    if (i == 0)
    {
        console.log("Blast Off!");
    }
    else
    {
        console.log(i);
    }
}

So the for loop starts at i = 10; then for all numbers > than or = to 0, counting down in each loop i--.

The if else statement says when/if i gets to 0 (i==0) the program will log to the console the string "Blast Off".  For all other numbers that i is == to (each number ( i )) just log to the console that specific number.

*Note: Use the double = (==) to mean equals.  I sometimes forget too so don't worry if you did remember.  Since this is javascript, make sure the { has a closing } and that you use semi colon ; in the right place.

Next we will use this knowledge to solve FizzBuzz.

Post comments or questions below.  Have fun with it and most importantly learn some new code today.

Brought to you by KENYACODE. Can you code?

No comments:

Post a Comment