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?
I donât see a âloopâ in your code 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.
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.
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:
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 We donât care about any other values/variables. They donât matter as far as this infinite while loop is concerned.
Any idea how I would make the loop start with an onclick?
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