Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    why does this code have an infinite loop

    Code:
    	var sentence = "|zero|one|two|three|four|five|six|seven|eight|nine|"
    	var numbers = sentence.split("|");
            var input = parseInt(window.prompt("Enter a number"));
               
            for(i=0;i<numbers.length;i++)
    	{
    		if(i=input)
    			{
    			document.write(numbers[i+1]);
    			}
    		else
    		{
    		i++;
    		}
    	}

  • #2
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Because the following line is setting the value of i in the loop:

    Code:
    if(i=input)
    The code should be

    Code:
    if (i == input)
       document.write(numbers[i+1]);

  • #3
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    If you are wanting to assign the value of the user's input to a variable then it should not be called i.
    Last edited by DougMck; 10-26-2006 at 03:34 AM.

  • #4
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Also why are you doing i++ in the else block? i will be incremented automatically on the next iteration of the loop.

  • #5
    New Coder
    Join Date
    Oct 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    uhh

    basically what i'm trying to do is make it so that if the user inputs 1, it outputs one.
    9 = nine
    8 = eight

    and so on.

    Can you help me with this?

  • #6
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts

    You're on the right track

    G'day.

    Basically you're on the the right track, the only things that you need to do are to make the numbers variable an array and to change the i=input to i == input.


    Give this a try:

    Code:
    <html>
    <body>
    
    <p>Number to name test script. Click the button to begin.</p>
    
    <input type=button onclick='getNumber();' value='Click Me'/>
    
    <script>
    var sentence = "|zero|one|two|three|four|five|six|seven|eight|nine|"
    var numbers = new Array();                // Must be an array
    numbers = sentence.split("|");            // Load array with data.
    var matched = 0;
    
    
    function getNumber()
    {
       // Get input from user
    
       var input = parseInt(window.prompt("Enter a number", ''));
    
             
       // Loop through numbers array looking for match
      
       for(i=0;i< numbers.length; i++)
       {
          if(i==input)                           // If match found then do something.
          {
             alert('You entered ' + numbers[i+1]);
             matched = 1;
          }
       }
    
       if (matched == 0)                    // Possibly do something if there was no match.
          alert('The number you entered is not recognized.');
    }
    </script>
    </body>
    Cheers,
    DouG.

  • #7
    New Coder
    Join Date
    Oct 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    LoL

    I have no doubt that your method would work. I love you for putting in the time.

    However, my javascript vocabulary is limited; since I am submitting this for a computer science lab, I need to work within what I am supposed to know.

    I'm gonna see if yours works. Thanks anyway.

  • #8
    New Coder
    Join Date
    Oct 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    damn

    it works like a charm. Grr. I wanna know what im' doing in JS. Now only if I can translate what you did into something I could pass off as knowing...

    (fyi, i already figured this out, but forgot it, so maybe this'll help me actually learn it for reals)

  • #9
    New Coder
    Join Date
    Oct 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I think I see my confusion:

    alert('You entered ' + numbers[i+1]);

    Let me see if I understand this:
    this is going to put out the string 'you entered' concatenated with the element and i, which is whatever element is equal to the users input, (number four, since its actually the 5th element, must be incremented by one in order so i can display five; number 0, since its actually the first element, must be incremented by one so it can reach the first element, which is zero.

    AH. I was wondering why this formula worked for 0. LoL. Only after I wrote this i think i understand ....(obviously, you can see my newbness now).

  • #10
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    No worries mate - glad I was able to help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •