Javascript: Javascript Programming For Absolute Beginners: Ultimate Guide To Javascript Coding, Javascript Programs And Javascript Language (4) by William Sullivan

Javascript: Javascript Programming For Absolute Beginners: Ultimate Guide To Javascript Coding, Javascript Programs And Javascript Language (4) by William Sullivan
Author: William Sullivan [Sullivan, William]
Language: eng
Format: azw3, epub
Published: 2017-10-16

answeranswerstart _value

10=1Q5

The last line just writes the answer out. To see better what is happening here, add in the following document.write to the curly brackets in the loop:

answer = answer + start_value;

document.write( "answer= " + answer + " start value = " + start_value + "<BR>");

Now, when you run the code, you should see this in your browser:

answer= 1 start value = 1

answer= 3 start value = 2.

answer= 6 start value = 3

answer= 10 start value = 4

answer= 15 start value = 5

answer= 21 start value = 6

answer= 2.8 start value = 7

answer= 36 start value = 8

answer= 45 start value = 9

answer= 55 start value = 10

By the time we get to the end, we should have an answer of 55, after going around the loop 10 times and adding up each time.

Exercise

Print the numbers 1 to 20 using a for loop

Exercise

Print the odd numbers between 1 and 20 using a for loop. You will need to make use of modulus here, as well as an IF statement. The loop should look like:

for ( start_value; start_value < end_value; start_value++ ) {

if ( ) {

document.write("answer= " + start_value + "<BR>");

}

}

It’s up to you to fill in what goes in between the curly brackets!