JavaScript looping

1

I'm learning javascript and now I'm starting to learn how to do looping, and I noticed that it's heavily used var i and when it's for / in var x why use those letters? I can use words in their place and it will work normally.
Can I put for inside another? Would that cause a problem?
When while should be used and what is the difference between while and do/while ? I was trying to understand a little bit about this by reading in w3schools , but it's not very clear.

    
asked by anonymous 09.08.2018 / 20:25

1 answer

3

It does not have a pattern and I've never noticed this variation from i to x because of that, there are some like i (me), some like x (I think it's for something else) , and of course there are those who must have adopted the one-in-one type of loop and another in the other type. I do not see a reason for this, who does it is that I should explain why it does, but I already kick that it's just like it.

You can use whatever you want, there is only one convention to use a simple letter because in general it is only there because of the looping mechanism and is not part of the problem domain, so it is up to a way of differentiating and avoiding emphasizing their presence. You can use contador as some do, they look horrible and I see it as a decrease in readability, bringing verbosity to something that does not matter.

Even better when you can use a loop construct that does not need a control variable and it already sets a variable for the item being parsed at that step.

You can do for within the other and you can use i , j , k , l (although it can be counted with 1 (one), or other letters. scenarios may be that x , y , w , z are more appropriate if you are talking about axes, just as there are cases for m and n , or a , b , c , d , e , but are rarer. i and j are triggered the most common in codes.

The only problem with a for within the other is that you get to have an exponential algorithm (complexity represented by O (N 2 )) and this is not very desirable, but it is not so tragic and there are cases that is the only way to do it. And if it's in low volumes it should not make that much difference. Just be sure you really have to do it this way.

About while has already been answered: What is the difference between the while, for, while and foreach? and What is the usefulness and importance of "do ... while"? . I do not know if it's still true, but they say W3Scools is not a good source.

    
09.08.2018 / 20:35