Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  24] [ 1]  / answers: 1 / hits: 20233  / 13 Years ago, sat, february 18, 2012, 12:00:00

Here is my code. I am expecting the number is 1, the number is 2... to be out putted up to 5 but all that is outputted is the number is 0 not sure why.



<script>
var i=0;
function test(){

for(i=0;i<=5;i++){
return the number is + i;
}
}
</script>
<script>
document.write(test());
</script>

More From » javascript

 Answers
18

return the number is + i;
It (the 'point' of script execution) returns back from the function with the first loop at i = 0



Write it as http://jsfiddle.net/hNWrg/



function test(){
var out = '';
for(var i=0;i<=5;i++){
out += the number is + i + <br>;
}
return out;
}

[#87385] Friday, February 17, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trentb

Total Points: 261
Total Questions: 101
Total Answers: 90

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
;