1

Ladies and gentlemen,

I'm stuck. I've been pondering this (and obviously have failed since I'm asking for your valuable assistance) in trying to get my code to work.

I need to come up with a simple (...I'm sorry, i'm new to this) code that prompt users to keep entering names using a loop. If the user does not enter 'q'(without quotes) and if the value entered is NOT null, then the value entered should be added to the array (in my case, names).

If the user enters 'q', the loop should stop, 'q' will not be entered in the array and the list of names should be printed (through the second function in my code).

Here's what I have so far... I can make the code work if I tell the loop to run i<5... it runs 5 times and then it stops. But it fails if i do i < names.length..it causes it say that length is null or not an object (on line 10). That's problem one. And for the life of me, I can't figure out how to add the logic that will run the loop until user enters q.

Please help!

Thank you.

function getNames(){
var names = new Array();
    for(i=0;i<names.length;i++){ /*if i do i=0;i<5;i++, the code works; it doesn't with this*/
    names[i] = prompt("Enter an item to add to the Name list (enter \'q\' to quit","");
}
printNames(names);
}

function printNames(names) {
for(x=0; x < names.length;x++){
document.write(names[x] + '<br />');
}

}
getNames();
printNames();

2 Answers 2

2

I am sure somewhere in your class/book it talks about while loops. So you want to use a while loop if you want them to keep entering without a limit.

while (myCondition===true) {
  //do something
}

Now look at your for loop and figure out why it is failing.

for(i=0;i<names.length;i++)

Look at what it is doing:

  • i = 0
  • names.length = 0

Is 0 < 0?

Sign up to request clarification or add additional context in comments.

5 Comments

Fair enough. But have you any input on why my code above fails to run when i enter use i < names.length but not when i say i < 5? I need it to run as long as there is at least one item in the array.
Edited it to answer your question from the comment.
+1 for letting the OP being able to learn by finding the "Answer" by himself =)
Oh duh! I printed it out and it was easier to see.. for some reason. SO i changed that to i >= names.length and that ran the code perfect. I was able to figure out the while condition, too. Thank you for the enlightenment!
Update, to improve it, I used a DO WHILE loop and that worked fantastically well. Within the DO WHILE I created an IF statement that adds value to array only if the input has a value (so > than '' ).
0

Well to start with Problem 1:

Your names array begins with a length property of 0 and so your first for loop doesn't run because 0 is not less than 0.

Which leads to Problem 2:

Again since nothing was entered into your names array your second for loop again does nothing and doesn't execute document.write because the length property of your array is still 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.