+ 2
condition for( X; A;C )
Hi, guys. please Help me. why the output is just (10) rather than it gives me 1 to 10 1 2 3 4 5 6 7 8 9 10 <body> <form> Enter the Number : <br /> <input type="number" id="number" /> <br /><br /> <br /> <textarea id="answer" cols="20" rows="10" placeholder="Answer Here"></textarea><br /> <input type="button" onClick=" secondFunction()" value="Click to get The answer" /> </form> <script> function secondFunction(){ for(var a=1; a<=10; a++){ document.getElementById("answer").value=a; } } </body>
3 Respostas
+ 3
Here I changed your script a little bit, as mentioned in previous answer you are overwriting the textarea on each loop iteration, instead you could either concatenate the previous textarea content, or use temporary buffer and flush it to textarea after loop finished.
<script>
function secondFunction(){
// Get the number in text input, you may
// need to anticipate invalid input here
var limit=parseInt(document.getElementById("number").value);
// Use result as temporay buffer to write
// the output into, this way we write the
// textarea only once.
var result ="";
for(var a=1; a<=limit; a++){
result += a + "\n";
}
// Write the result into textarea
document.getElementById("answer").value=result;
}
</script>
+ 3
😍 thanks a lot
+ 2
you are assigning the value of "a" to it which is incrementing in your loop.