For loop minus number problem

  • beautyfiend
  • Newbie
  • Newbie
  • beautyfiend
  • Posts: 8
  • Loc: London

Post 3+ Months Ago

Hi, the function minusNumber of my code is not working and I don't know why :? Any help much appreciated! thanks.
Code: [ Select ]
<html>
    <head>
 
    <script language="javascript">
 
function plusNumbers()
{
   for (i = 0; i <= 10; i++)
    {
alert("The number is +" + i)
 
    }
}
 
 
function minusNumbers()
{
   for (i = -0; i <= -10; i--)
    {
alert("The number is -" - i)
 
    }
}
</script>
 
    </head>
<body>
 
 
<form>
 
Press PLUS to display 0 to +10: <input type="button" value="PLUS" onclick="plusNumbers()" ><br/>
Press MINUS to display 0 to -10: <input type="button" value="MINUS" onclick="minusNumbers ()">
</form>
</body>
 
 
</html>
 
  1. <html>
  2.     <head>
  3.  
  4.     <script language="javascript">
  5.  
  6. function plusNumbers()
  7. {
  8.    for (i = 0; i <= 10; i++)
  9.     {
  10. alert("The number is +" + i)
  11.  
  12.     }
  13. }
  14.  
  15.  
  16. function minusNumbers()
  17. {
  18.    for (i = -0; i <= -10; i--)
  19.     {
  20. alert("The number is -" - i)
  21.  
  22.     }
  23. }
  24. </script>
  25.  
  26.     </head>
  27. <body>
  28.  
  29.  
  30. <form>
  31.  
  32. Press PLUS to display 0 to +10: <input type="button" value="PLUS" onclick="plusNumbers()" ><br/>
  33. Press MINUS to display 0 to -10: <input type="button" value="MINUS" onclick="minusNumbers ()">
  34. </form>
  35. </body>
  36.  
  37.  
  38. </html>
  39.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Well, first of all, there's no such thing as negative zero.

Second, your loop condition is starting the loop at 0, but telling it to only execute while i is less than or equal to -10. What you really want is for the loop to execute while i is greater than or equal to -10.

Code: [ Select ]
for( i = 0, i >= -10; --i ) {
  • beautyfiend
  • Newbie
  • Newbie
  • beautyfiend
  • Posts: 8
  • Loc: London

Post 3+ Months Ago

spork wrote:
Well, first of all, there's no such thing as negative zero.

Second, your loop condition is starting the loop at 0, but telling it to only execute while i is less than or equal to -10. What you really want is for the loop to execute while i is greater than or equal to -10.

Code: [ Select ]
for( i = 0, i >= -10; --i ) {



I put that code in and it is still not coming up with anything, pehaps because of
Code: [ Select ]
alert("The number is -" - i)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Change that to
Code: [ Select ]
alert("The number is " + i);
  • beautyfiend
  • Newbie
  • Newbie
  • beautyfiend
  • Posts: 8
  • Loc: London

Post 3+ Months Ago

I'm sorry, I don't know what's going on, I'm now getting no alert boxes. My code at the moment is
Code: [ Select ]
<html>
    <head>

    <script language="javascript">

function plusNumbers()
{
for (i = 0; i <= 10; i++)
    {
alert("The number is +" + i);

    }
}


function minusNumbers()
{
for( i = 0, i >= -10; --i )
    {
alert("The number is " + i);

    }
}
</script>

    </head>
<body>


<form>

Press PLUS to display 0 to +10: <input type="button" value="PLUS" onclick="plusNumbers()" ><br/>
Press MINUS to display 0 to -10: <input type="button" value="MINUS" onclick="minusNumbers ()">
</form>
</body>


</html>
  1. <html>
  2.     <head>
  3.     <script language="javascript">
  4. function plusNumbers()
  5. {
  6. for (i = 0; i <= 10; i++)
  7.     {
  8. alert("The number is +" + i);
  9.     }
  10. }
  11. function minusNumbers()
  12. {
  13. for( i = 0, i >= -10; --i )
  14.     {
  15. alert("The number is " + i);
  16.     }
  17. }
  18. </script>
  19.     </head>
  20. <body>
  21. <form>
  22. Press PLUS to display 0 to +10: <input type="button" value="PLUS" onclick="plusNumbers()" ><br/>
  23. Press MINUS to display 0 to -10: <input type="button" value="MINUS" onclick="minusNumbers ()">
  24. </form>
  25. </body>
  26. </html>
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Do:

Code: [ Select ]
<html>
<head>
   <script type="text/javascript">
      window.onload = function()
      {
         var plusButton = document.getElementById('plusButton');
         var minusButton = document.getElementById('minusButton');
 
         plusButton.onclick = function()
         {
            for(var i = 0; i <= 10; i++)
            {
               alert("The number is: " + i);
            }
         }
 
         minusButton.onclick = function()
         {
            for(var i = 0; i >= -10; i--)
            {
               alert("The number is: " + i);
            }
         }
      }
   </script>
</head>
 
<body>
   Press to display the numbers 0 - 10: <button id="plusButton">Click!</button>
   <br /><br />
   Press to display the numbers 0 - -10: <button id="minusButton">Click!</button>
</body>
 
</html>
  1. <html>
  2. <head>
  3.    <script type="text/javascript">
  4.       window.onload = function()
  5.       {
  6.          var plusButton = document.getElementById('plusButton');
  7.          var minusButton = document.getElementById('minusButton');
  8.  
  9.          plusButton.onclick = function()
  10.          {
  11.             for(var i = 0; i <= 10; i++)
  12.             {
  13.                alert("The number is: " + i);
  14.             }
  15.          }
  16.  
  17.          minusButton.onclick = function()
  18.          {
  19.             for(var i = 0; i >= -10; i--)
  20.             {
  21.                alert("The number is: " + i);
  22.             }
  23.          }
  24.       }
  25.    </script>
  26. </head>
  27.  
  28. <body>
  29.    Press to display the numbers 0 - 10: <button id="plusButton">Click!</button>
  30.    <br /><br />
  31.    Press to display the numbers 0 - -10: <button id="minusButton">Click!</button>
  32. </body>
  33.  
  34. </html>
  • beautyfiend
  • Newbie
  • Newbie
  • beautyfiend
  • Posts: 8
  • Loc: London

Post 3+ Months Ago

Nightslyr wrote:
Do:

Code: [ Select ]
<html>
<head>
   <script type="text/javascript">
      window.onload = function()
      {
         var plusButton = document.getElementById('plusButton');
         var minusButton = document.getElementById('minusButton');
 
         plusButton.onclick = function()
         {
            for(var i = 0; i <= 10; i++)
            {
               alert("The number is: " + i);
            }
         }
 
         minusButton.onclick = function()
         {
            for(var i = 0; i >= -10; i--)
            {
               alert("The number is: " + i);
            }
         }
      }
   </script>
</head>
 
<body>
   Press to display the numbers 0 - 10: <button id="plusButton">Click!</button>
   <br /><br />
   Press to display the numbers 0 - -10: <button id="minusButton">Click!</button>
</body>
 
</html>
  1. <html>
  2. <head>
  3.    <script type="text/javascript">
  4.       window.onload = function()
  5.       {
  6.          var plusButton = document.getElementById('plusButton');
  7.          var minusButton = document.getElementById('minusButton');
  8.  
  9.          plusButton.onclick = function()
  10.          {
  11.             for(var i = 0; i <= 10; i++)
  12.             {
  13.                alert("The number is: " + i);
  14.             }
  15.          }
  16.  
  17.          minusButton.onclick = function()
  18.          {
  19.             for(var i = 0; i >= -10; i--)
  20.             {
  21.                alert("The number is: " + i);
  22.             }
  23.          }
  24.       }
  25.    </script>
  26. </head>
  27.  
  28. <body>
  29.    Press to display the numbers 0 - 10: <button id="plusButton">Click!</button>
  30.    <br /><br />
  31.    Press to display the numbers 0 - -10: <button id="minusButton">Click!</button>
  32. </body>
  33.  
  34. </html>



Thank you! It works. :D

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 95 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.