Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  114] [ 6]  / answers: 1 / hits: 35073  / 7 Years ago, wed, february 15, 2017, 12:00:00

How to get the loop values outside the loop below example only prints the last value, For example if i wanted to print the loop results.



var result;

for (var i=0; i < 10; i++) {
result = i;
}

console.log(result);


Now how can i get the iterated values of the loop which are (1 to 10) in the console, because now i will only print the last value which is 10.


More From » loops

 Answers
68

Put the log statement inside the loop where you set the value.



var result;

for (var i=0; i < 10; i++) {
result = i;
console.log(result);
}


If you only want one output statement, you can concatenate your results before logging:



var result = ;

for (var i=0; i < 10; i++) {
result += i + ;
}

console.log(result);


This will output 0 1 2 3 4 5 6 7 8 9 10


[#58925] Monday, February 13, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;