How to loop the prompt?

Im working on this code as a way to better understand Javascript and im running into a problem.

Im trying to figure out on my own how to loop this entire code so that once user is finished recieving the alert, the entire code loops back to the beginning and asks for another input and you could thoretically do this forever bu running the switch statement again and again without having to refresh the page.

Im just doing this for fun so that I can better understand JS. I figured that this would be an easy tweak and I could figure out how to loop this on my own but everything I try breaks the code. Why is something so simple so unintuitive? can you help me loop this code so I can see how this works?

Thanks for any help.

I don’t see a “loop” in your code :slight_smile: You are calling prompt once and then passing the user input through the switch and then that’s it. What have you tried so far?

You said you want to figure this out on your own so I’m not going to give you any hints right now. But if you have no ideas I would suggest you use the googles, maybe search for something like “javascript infinite loop”?

yeah ive tried a number of things like putting another prompt before each break but I get why that didn’t work because those prompts aren’t linked to the other variables. I tried creating a function and calling that function at the end of the switch but that broke the code. I tried using:

while text = true { }

and wrapping the switch statement inside the curly braces.
The issue is that im not understanding what value I can grab to trigger the while statement. So the reason you don’t see any loops in the code is because I decided to omit the things that were not working to make what I was trying to do more clear.

im kinda understanding while loops. I believe that’s what I should use, but I don’t know how to grab anything to of value to put inside to make it work.

I think you might be onto something here :slight_smile:

Remember, I while loop continues to loop while the condition is true (or you intentionally break out of it inside the loop):

while (condition) {

As long as the condition is true the while loop will continue. I wonder what you could replace “condition” with that will always be true?

1 Like

Im getting it. But I have no idea what to pass in because I want the prompt to reoccur when text = a blank value essentially. right? because text = prompt. and the prompt assigns a value to text.

so im trying

while (text = “” ) {} , while (text = 0) { } , while (text = false) { } .

but no luck. im stumped at what to pass in as a condition.

Maybe I misunderstood what you wanted to do? I thought you wanted to keep prompting the user forever in an infinite loop. If so, why do you care about the value of text. Wouldn’t you want to continue looping no matter what they entered in the prompt? I think you just need to put something in there that is always true.

I want to prompt the user with the original prompt, after the alert is triggered. so Im wanting the program to run like this:

  1. (prompt) Enter a fruit to get a fruit fact!
    // user enters fruit via prompt input

  2. (alert) displays a fruit fact
    // loops back to 1.

  3. (Prompt) Enter a fruit to get a fruit fact!

  4. (alert) displays a fruit fact

  5. (prompt) Enter a fruit to get a fruit fact!
    //loop continues etc.

Im assuming that because my ‘text’ variable is assigned to the prompt, I need to loop the empty prompt to restart the loop.

this may work…

im thinking while the text is != to a string value, the loop restarts from the beginning?


I was putting my while loop AFTER the variables in my code. so if I put the

while ( text = true) {} in front of the variables, everything runs correctly and I get a infinite prompt and alert loop.

Thanks a Buch for your help. couldn’t have done it without you! :+1:

That is one way to do it since the assignment will return the value assigned to text and since you are assigning true the condition will always be true. But you don’t actually need the assignment in there, you can just do:

while (true) {

So it works without the (text = true) because all of the code is true? so the while (true) works because all values are true?

Remember, I while loop will loop as long as the condition is true:

while("condition is true") {

So if you want an infinite loop then you just have to stick something inside the parens that is always true. What value is more true than true :slight_smile: We don’t care about any other values/variables. They don’t matter as far as this infinite while loop is concerned.

1 Like

Got it, awesome! :grinning:

Any idea how I would make the loop start with an onclick? :thinking:

Im thinking if the while loop is looking for a true value I can somehow set the value of the code to false by default and let that clicked button change the value to true thus getting the loop to run? Maybe this is too advanced for where im at at the moment but ice love to learn how I would do something like this.

Wow got that onClick working. that was incredibly easy, I just had to think about it for a sec lol . Wrap the entire code in a function and let the onclick call the function :crazy_face:

1 Like

you are assigning true to text so text = true is evaluated to true

1 Like

GOOOT IT. That makes sense now. I tend to over think this stuff :+1: Thanks for breaking that down.