Are there any practical benefits to declaring multiple values in the statement 1 of your for loop? I can't think of anyone. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 10

Are there any practical benefits to declaring multiple values in the statement 1 of your for loop? I can't think of anyone.

The lesson below says I can declare multiple values and separate them with comas. https://www.sololearn.com/learn/JavaScript/1140/ There is an example of doing so in that lesson but it's very simple and not very useful. The second part called text is assigned to i. I can do the same with a simple variable declared outside the for loop. I tried. https://code.sololearn.com/W9jj0QlKU732/?ref=app

28th Dec 2020, 11:42 PM
Alex
Alex - avatar
5 Respostas
+ 12
Looks like you went in the right direction in the last section of your code. The reason you get an error is because you've typed a wrong character in the second statement of your for loop. Letter Ä· is different from k. Here's my solution: https://code.sololearn.com/WFKX4rqCrwIJ/?ref=app
29th Dec 2020, 2:07 PM
Dual Core
Dual Core - avatar
+ 10
Elizabeth Kelly I actually thought of your first reason. I tried it out in my code but failed due to my mistake. I think I simply pressed the k key for a bit too long and got a different character instead. I didn't notice that and assumed that incrementing/decrementing more than one variable was NOT possible. I have fixed that. Anyway thank you and rodwynnejones for answering my question!
29th Dec 2020, 9:13 PM
Alex
Alex - avatar
+ 9
Dual Core thanks a lot! You've helped me a lot. You are the best just like your answer šŸ‘šŸ‘šŸ‘
29th Dec 2020, 8:53 PM
Alex
Alex - avatar
+ 4
A valid reason is when you have more than one variable that needs to increment/decrement with each pass of the loop AND the variable has no purpose outside of the for loop. It can help simplify your code somewhat. Iā€™ve done this many times on Sololearn challenge solutions as well as in some of my app development projects.
29th Dec 2020, 12:45 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 3
Example of how I've used it, today actually, (although in C or C++, can't remember which) The poster wanted to separate an int array (with positive and negative values) into one array with the positive values and one with the negative values, so I initialise three int variables in the "for loop" and incremented them individually (the non-loop variables) in the if....else statements so that the values would be inserted into the correct array and into the correct index..
29th Dec 2020, 12:04 AM
rodwynnejones
rodwynnejones - avatar