Need help with an prompt to array in a while loop

my assignment

write a while loop that prompts user to enter name

add their names to an array

if they enter “exit” end the prompting

sort array and list in sorted order

this is what i got so far. sooo confused because
i cant get the user input into an array

var names = new Array();
var loopCounter;
loopCounter = 0;

while (x != “exit”)
{
var names[loopCounter] = prompt(“enter name”,“”);

loopCounter++

}

var i;

names.sort();

for (i=0;i<names.length;i++)
{
document.write(names[i] + “<br>”);
}

Using === instead of ==, and !== instead of != are the preferred comparisons to use in JavaScript. They help to avoid problems where the loose comparison operators match with unexpected terms.

You can use a one-and-a-half loop.


while (true) {
    myResponse = prompt("What's your favorite Lord of the Rings Character?\
\\rtype 'exit' to leave prompting","");
    if (myResponse === "exit") {
        break;
    }
    names[loopCounter] = myResponse;
    loopCounter++;
}

Or, you could use a different type of loop construct:


myResponse = '';
do {
    if (myResponse > '') {
        loopCounter++;
        names[loopCounter] = myResponse;
    }
    myResponse = prompt("What's your favorite Lord of the Rings Character?\
\\rtype 'exit' to leave prompting","");
} while (myResponse !== 'exit');

ok i figured most of it out now but now im wondering how do i ignore data or keep information someone has entered from entering the array.

it works now but when the user types “exit”. it goes into the array. i don’t want it to enter the array just exit the prompting. sorry for the dumb question, im taking this class online and theres is no examples of anything like this

<script>
function askMe()
{
var names = new Array();
var loopCounter;
loopCounter = 0;
var myResponse;
var nowDate = new Date();
var currentDay = nowDate.getDate();
var nowMonth = nowDate.getMonth();
var nowYear = nowDate.getFullYear();
var months = new Array();

while (myResponse != "exit")
{
	myResponse = prompt("What's your favorite Lord of the Rings Character?\

\rtype ‘exit’ to leave prompting",“”);

	names[loopCounter] = myResponse;
	
	loopCounter++;
	
}

var i;

names.sort();

for (i=0;i&lt;names.length;i++)
{
	document.write("&lt;li&gt;" + names[i] + "&lt;br&gt;");
}

document.write("&lt;br&gt;" + "&lt;b&gt;Todays Date:" + [nowMonth+1] + "/" + currentDay + "/" + nowYear + "&lt;br&gt;" + "&lt;br&gt;&lt;/b&gt;");
document.write("Press the Reload/Refresh Button to ask again");

}
</script>

omg the do while loop is it! and that was in my notes stupid me sigh thank you so much for the help and yeah now i understand. took me awhile to figure out why prompt had to be below the if statement but now i understand. thanks to everybody for helping me

Your while loop depends upon the value of x, which doesn’t appear to be declared or referenced anywhere else.
Use the error console, which will be trying to tell you this.

oooh ok duh lol. so i changed x to names
because if they type exit it should stop right?
error console now says i need ; before statement var names[loopCounter] = prompt(“enter name”,“”);
i cant find where i need to add another ;??

var names = new Array();
var loopCounter;
loopCounter = 0;

while (names != “exit”)
{
var names[loopCounter] = prompt(“enter name”,“”);

loopCounter++
}

var i;

names.sort();

for (i=0;i<names.length;i++)
{
document.write(names[i] + “<br>”);
}

while (names != "exit")

Now you’re comparing an array to a string, which doesn’t make sense. Clearly you know how to subscript an array, so do it.

Depends on if the tools needed for the assignment were adequately explained.

I can see what’s wrong with it, but I wouldn’t have understood why exactly 2 months ago. Sicbag, start with just the beginning chunks of your code and make liberal use of alert() or console.log or whatever you like to see what’s going on at any particular point in your code. What happens if you do alert(names)? You’ll see the problem right away.

I don’t think we’ve gone over triple +++ or !== what does that mean? but ya im lookin through my notes again just in case