Skip to main content

Posts

Showing posts with the label while loops

JavaScript Project Study: How To Create A Simple Todo List APP?

After I learned JavaScript loop and array, it's time to implement those codes into a simple project and see how it works. The course instructor leads us to create a small To Do List APP. Basically, when we open the web page, it will pop up a question:"What would you like to do?". Then we enter "new" and add a todo. If we want to check our todo list, we need to enter "list", so we can see our todos are stored in the console. The question will ask you again and again until you enter "quit" to end it. We create a HTML page and link to a separate JS file.  In the JS file, we start to write a  todos array . We leave the empty[] since we will enter data through the pop up question window.  Then, we create the pop up window by writing a prompt("What would you like to do?" So when we open the web page, it will pop up this question to ask you to enter your todos. Next, we use while loop to let the site keep ask...

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...