Hi, can you explain the fifth step, I couldn’t understand it.
This is the article link
Let me try to give some explanations:
“After (contacts.length-1)
iterations” means “When we enter the last iteration”. For example: Four contacts (contact.length
: 4) need four iterations. Therefore contacts.length-1
is three. So “after three iterations” means we now enter the fourth and last iteration.
Now, if we are on our last iteration this means we haven’t found a match, yet. Otherwise we would have already left the loop because of the break
statement. There are two possibilities:
break
out of the loopThis sounds like we need to upgrade our if
statement from 4.3. to an if...else
statement, right? Inside the else
part we set the paragraph text to “Contact not found”. Because we only want to set this text in the last iteration, we also need a if
inside this else
. In the end the structure looks something like this:
if(/* name is found */) {
/* write text to paragraph */
break;
} else if(/* it is the last iteration */) {
/* write "Contact not found." to paragraph */
}
The last part of (5.) just means: “let the loop finish after we wrote ‘Contact not found.’ (no break
needed)”.
I hope this clears it up a bit and didn’t make it more confusing
If you have any further questions, feel free to ask.
That was perfect, thank you very much.
But it makes me greedy, to request an example for another paragraph, I really couldn’t understand her example.
Paragraph link
Glad I could help
First a small note to the difference between break
and continue
. As an analogy you could look at them as two brothers where break
is the lazier one. Both leave work early on Tuesday (skip the rest of the current iteration), but brother continue
will show up the next day (iteration) and work for the rest of the week (loop). Whereas brother break
won’t show up for the rest of the week (exit the whole loop).
Now for the example: I think the hardest part to understand is (Math.floor(sqRoot) !== sqRoot)
. Math.floor
rounds down every number to the next integer:
Math.floor(5.22)
-> 5Math.floor(5.99)
-> 5Math.floor(5)
-> 5As you can see only in the last example is the number we put in the same number that comes out. They are only the same, when we put in integers (e.g. 5, 13, 1), but always different when we put in decimal numbers (e.g. 1.43, 3.9, 15.1). So basically if (Math.floor(sqRoot) !== sqRoot)
askes the question: “Is the variable sqRoot
not an integer?” or “Is the variable sqRoot
a decimal number?”.
In the example we only want to write down integers. So by using continue
we skip the last line inside the loop and continue with the next number (iteration) when we see a decimal number.
One could rewrite this loop without continue
:
for (let i = 1; i <= num; i++) {
let sqRoot = Math.sqrt(i);
if (Math.floor(sqRoot) === sqRoot) {
para.textContent += i + ' ';
}
}
Now the if
statement asks: “Is sqRoot
an integer?” If it is an integer, the number gets written down.
Have a nice day!
I am really grateful, thank you very much.