Jump to content

Why Doesn't This Work?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
5te4lthX

5te4lthX

    Newbie

  • Members
  • Pip
  • 7 posts
<html>
<head>
<script type="text/javascript">
function loop(a)
{
 for (i = a; i <= 5; i++)
 {
 document.write("The number is " + i);
 document.write("<br />");
 }
 document.write("<input type=\"button\" onclick=\"loop(-1)\" value=\"click\" />");
}
</script>
</head>

<body onload="loop(0)">
</body>
</html>

I just started learning Javascript, and I don't understand why this isn't working properly. It displays numbers from 0 to 5 when the page loads and a button that when clicked, should add a -1 to the list. However, when the button is clicked, the -1 isn't added to the list. Could anyone explain to me why this happens?

#2
wahkiz

wahkiz

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Let's just say.. you didnt break your loop and you tried to initiate another =D.

And why are you putting the entire html in a javascript. Lol.

If you just want to add a -1 into the list. Just make another function that prints out -1. Then execute it.