...

View Full Version : why does this code have an infinite loop



thenextbesthang
10-26-2006, 01:33 AM
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++;
}
}

DougMck
10-26-2006, 03:25 AM
Because the following line is setting the value of i in the loop:



if(i=input)


The code should be



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

DougMck
10-26-2006, 03:28 AM
If you are wanting to assign the value of the user's input to a variable then it should not be called i.

DougMck
10-26-2006, 03:32 AM
Also why are you doing i++ in the else block? i will be incremented automatically on the next iteration of the loop.

thenextbesthang
10-26-2006, 04:28 AM
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?

DougMck
10-26-2006, 05:01 AM
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:



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

thenextbesthang
10-26-2006, 06:13 AM
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.

thenextbesthang
10-26-2006, 06:16 AM
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)

thenextbesthang
10-26-2006, 06:29 AM
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).

DougMck
10-26-2006, 08:55 PM
No worries mate - glad I was able to help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum