Skip to main content

Posts

Showing posts with the label js exercise

JavaScript Basic: While Loops

According to the course, loops refer to DRY (Don't Repeat Yourself) . In the other word, we want to keep our code as DRY as possible. Loops offer a easy way to run the code repeatedly and save us a lot time. While Loops While loops is one kind of loops. It allows to repeat code While a condition is true . while (someCondition) { //run some code } While loops is very similar to an if statement, except it repeats a given code block instead of just running it once. For example: var num = 1; while(num <= 10) {           console.log(num);           num += 2; } The example assumes a number is equal to 1 and gives a condition we run the (num +2) and count how many result are less and equal than 10. When I run the loop in the console, the result: The (num + 2) result less than 10 are 1, 3, 5, 7, 9. And it ends by 11 since 11 is greater than 10. Infinite loops occur when the terminating condition in a loop is nev...

JavaScript Basic: Prompt & Console.log Exercise

Today the course assigned a simple JavaScript exercise: Here is my steps: Step One: Create a HTML page and link to the JavaScript File I create a simple HTML page and give a title "JS Stalker Exercise". Then, I link the HTML page to a new JS file named "stalker.js". Step Two: Enter variable data in the JS file In the JS file, I start to write three prompt messages which are asking for user's first name, last name and age. In order to print out user's full name and age in separate sentences, I set two console.log message. The only thing we need to pay attention is the spacing between each word.  The print sentence has one small space between each word : Your full name is X X. Therefore, in the console.log, we need to set the right space for each word. Step Three: Test the result When I open the page, it asks me three pop up questions in orders: After I answer each questions, I can see the print sentences in...