Calculate the sum of odd numbers greater than 9 and less than 30 a for-loop .
Solution
Explanation
To accomplish our task, we should remember challenge #02 because back then, the task was to print out every odd number below 99. That means we create a for loop with an increment of 2 after each iteration to get only odd numbers when starting with an odd number. In our case with 11.
Remembering challenge #05 makes us also happy again. There we had the task to create the sum of the numbers from 1 to 24. The same concept applies to this task. We add up every odd number above 9 and below 30 to a sum.
After the loop has finished, we print it out to the console:
Accumulated value printed out to the console (Sum of all odd numbers >9 & <30)
Follow me on Instagram and don’t miss the latest Challenge!
To create the factorial of 9 we make almost the same as in challenge #05 but with a different loop body code block.
Last time we used this statement to gather the accumulated sum sum+=i. Knowing a bit about mathematics means you are aware of the fact that a factorial is nothing more than a chained multiplication.
So 9! is nothing more than 9*8*7*6*5*4*3*2*1. Together with the knowledge about for-loops we can draw advantages out of this by using the running index of i to create a factorial. To get our desired result, we iterate exact 9 times!
Result of 9! printed out to the console
Follow me on Instagram and don’t miss the latest Challenge!
Print all the multiplication tables with numbers from 1 to 10 with a for-loop.
Solution
Explanation
With this challenge, a function is the best choice to accomplish the task. If you aren’t familiar with functions. No reason to worry.
But first, if you accomplished this task with 10 different for loops. Your solution isn’t wrong. The code does what it should do and hopefully right then. To enter the next level: 10 for-loops aren’t a good thing. Below I got a link to a Medium article of mine to show you more coding techniques that aren’t good.
Coming back to our solution. Code repetition is not a good practice. Even though the code does things slightly different. A rule of thumb: If two parts of source code look the same in 50% of lines, merge them into one part.
Avoiding code repetition means having reusable tools. A function suits that perfectly. A function provides input parameters. These are the settings for the function to operate with. Inside the body of the function, you have the reusable code pieces that operate slightly differently, depending on what you’ve put into the function as a parameter.
Now really back to the solution: Having a for-loop to with a function call to printTable() inside will execute the function 10 times since we configured the head of the for-loop to do it 10 times. The function printTable() has an input parameter of n. The for-loop passes the variable i every iteration, respectively, the values of 1 to 10.
You can now assume what the function must do. Right, the body is almost identical to the previous challenge, where you just print out one multiplication table. We changed the hard-coded 6 to an n and linked this variable to the input parameter of the function, resulting in a function that prints out the multiplication table of the number we pass to it as a parameter.
Because the function does it for any number and once per call, we can just call it ten times with corresponding parameters and we got our solution right away!
Part of the Solution’s Console Printout
The line of console.log(""); just prints out an empty line as a divider for the multiplication tables.
Follow me on Instagram and don’t miss the latest Challenge!