JavaScript Challenge  Internship to CEO #04

JavaScript Challenge Internship to CEO #04

Task of Challenge #04/54

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!

https://www.instagram.com/arnold.code/

Find out what many programmers do wrong and be better!

Here is a pre-picked and high quality Medium article for you!

JavaScript Challenge  Internship to CEO #03

JavaScript Challenge Internship to CEO #03

Dieses Bild hat ein leeres Alt-Attribut. Der Dateiname ist Cover-Pic.png

Task of Challenge #03/54

Print a multiplication table with 6 with for-loop .

Solution

Expected result:

The console output of the solution

Explanation

The row of six goes from 6 to 60. Therefore we need a for-loop that iterates 10 times. You are a bit familiar with loops now (since you made challenge #01 and #02) and you know the rules of a for-loop.

The variable row is our printed statement every iteration. To get the multiplication table of six, we just multiply the base number of 6 with the next value of the iteration process. The row of six starts with 6*1, then 6*2, and then 6*3, and so on…

To print now a decent table, we have to display the entire entry as a term. So 6 * 1 = 6 as code is "6 * " + i + " = " 6 * i We need to have the quotation marks here, because we want a string as result. If we don’t use the quotation marks the „compiler“ would then complain about the = . Leaving out also the = and the + sign in front of it would result in the following line:

let row = 6 *  + i + 6 * i;

Running the program then will print this:

Result of misused syntax

We have created a completely different term and therefore also a completely different output.

Be sure, that you know what you want to accomplish in order to use the right data types and statements.

Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

Are you interested in Object-Oriented Programming Languages?

Here is a top and high quality Medium article for you.

JavaScript Challenge  Internship to CEO #02

JavaScript Challenge Internship to CEO #02

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.

JavaScript Challenge  Internship to CEO #01

JavaScript Challenge Internship to CEO #01

Task of Challenge #01/54

Print numbers from 1 to 10 with a for-loop .

Solution

Explanation

We just need to print out ten numbers to the console prompt. A for-loop is the perfect tool for this job! We create a loop with a running index of i with the starting value of 1.

The loop will stop once i reaches the value of 11, so when i equals 10, the loop will execute the last execution because we created the condition of i <= 10.

The running index will be increased by 1 every iteration. i++ is the corresponding statement for that. It is the short form for i = i+1.

Inside the loop we got our statement of console.log(i); which prints out, whatever is inside the brace, to the console window.

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.

Template for The JavaScript Internship to CEO Challenge #00 – The Challenge Template

Template for The JavaScript Internship to CEO Challenge #00 – The Challenge Template

Introduction & Tutorial

If you are new to the challenge or don’t know how to start, this is the template you can download and take part in my coding challenge JavaScript – Internship to CEO.

Template

Just download the template, write your code into the challengeHere.js open the index.html in chrome or any other browser.

Open Console

Chrome: Hit F12-Key to open console.

Safari: Option + ⌘ + C.

Internet Explorer: Hit F12-Key to open console.

Firefox: Hit CTRL + SHIFT + K to open the Web console (COMMAND + SHIFT + K on Macs).