Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: why does this code have an infinite loop

1. ## 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++;
}
}```

• 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]);```

• If you are wanting to assign the value of the user's input to a variable then it should not be called i.

• Also why are you doing i++ in the else block? i will be incremented automatically on the next iteration of the loop.

• ## 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?

• ## 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.
{
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.

• ## 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.

• ## 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)

• ## I think I see my confusion:

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).

• 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
•