So far we have learned about conditionals, where we can add an if statement and have it go one way, or else go the other. We have also seen if else and else if.
But what if we wanted to do something like this:
..Something where we keep doing it over and over. This is known as looping.
If I can demonstrate:
They are many ways to do a Loop and one of the main ways is a for Loop. Heres the syntax:
So i starts off with 0. We have a semicolon, which means that’s the end of the expression; and then in the next line it says: i which is at this moment 0, if it is less than the todos.length(5), then run this loop.
When you’re done, do i++ — which means i plus 1.
Now if we wanted to add an exclamation mark on the end of the todos, we can access todos by:
So, now I can just do [i] + “!”
But if I do todos— my todos don’t have this exclamation mark we’ve just console logged.
So let’s do it the proper way.
And what if we want to delete some items from the list?
The while loop
The while loop goes something like this:
We are adding; so every time we’re increasing counter by 1. The loop stops as soon it reaches 10 because after 10, it is no longer right.
do while Loop
So what is the difference here?
The difference is that with a while loop we check the condition first, versus the do while loop that says: “Do the stuff first and then do counterTwo”.
And also the action that we want to perform on the todos can be represented in a function and it will give the same results.