Task of Challenge #02/54
Print the odd numbers below than 99 with a for-loop
.
Solution #01
Explanation #01
To get the expected result, we start the running index i
with the value of 1 and have in mind that in between every odd number comes an even number. Therefore, we increase our running index i
by 2 every iteration with i += 2
.
We set the condition to <=99
because we only want to print out odd numbers below 99, i.e. the odd numbers between 1 to 97.
Inside the loop we got our statement of console.log(i);
which prints out, whatever is inside the brace, to the console window. In our case the wanted odd number.
Solution #02
Explanation #02
When starting from 97, which is an odd number respectively, and iterate backwards, we almost got it. To get every odd number, we remember ourselves that in between every odd number comes an even number. Jumping over them by decreasing the running index i
to get every odd number.
We have to change the conditions for the for-loop
. First i
starts at 97, then our condition has to be i > 0
. Not i >= 0
, otherwise we would print out the even number 0 (Yes 0 is an even number). The running index gets decreased by 2 after every iteration with i -= 2.
Inside the loop we got our statement of console.log(i);
which prints out, whatever is inside the brace, to the console window. In our case the wanted odd number.
Follow me on Instagram and don’t miss the latest Challenge!
https://www.instagram.com/arnold.code/
Gather more Knowledge about JavaScript?
Here are two pre-picked and high quality Medium articles for you.
Document object model Free DOM Guide for everyone!https://t.co/yjO6OMy3uy#HTML #html5 #htmlcss #htmlcode #htmlemail #htmlcoding #medium #tutorial #Tutorials #WritingCommunity #webdesign #website #web #webdesigner #webdevelopment #WebDeveloper #code #coder #CodersTeach #coder
— ArnoldCode (@ArnoldAbrahamP1) December 23, 2020
Quick 3-Minute Guide for the Monday Morning about https://t.co/thEjiQhZR6() @JS_PlainEnglish https://t.co/thEjiQhZR6() https://t.co/Fg4ky3xvn9 #javascript #learning #coding #Frontend #Developer #programmer #Guide #tutorial #Fastlane #QuickHits #easyaccess #learntocode
— ArnoldCode (@ArnoldAbrahamP1) March 29, 2021