Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Devaka Cooray
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# Infinite Loop problem

Ranch Hand
Posts: 153
2
I am trying to work on a simple HTML program with javascript that has the user guess a number between 1-100 and then tries to narrow down the number until they get it correct

1.e.
Random number is 27
guess #1 = 50 ... next guess is 1-50
guess #2 = 25 ... next guess is 25-50
guess #3 = 37 ... next guess is 25-37
and so on... but I get caught in an infinite loop and I am not sure why... any suggestions?

Marshal
Posts: 67226
169
Your click handler checks the answer by going into a loop. If the answer is wrong, how does the user get a chance to enter another guess?

Hint: ponder on why the loop is there in the first place.

John Morgan
Ranch Hand
Posts: 153
2
Yup, I got that far, the loop is there to keep the count of the number of guesses and the high and low. I think I need to add the checkGuess inside the while loop but I am not sure who I could do that, and if I recall the function again I will reinitialize all the variables.

Bear Bibeault wrote:Your click handler checks the answer by going into a loop. If the answer is wrong, how does the user get a chance to enter another guess?

Hint: ponder on why the loop is there in the first place.

Bear Bibeault
Marshal
Posts: 67226
169

Why is the loop there in the first place?

John Morgan
Ranch Hand
Posts: 153
2
I know there are other ways to do this (i.e. just using if statements) however I wanted to work on while loops in javascript and this seemed like a simple project to do the count down. The idea is to put a user interaction in the while loop instead of just having it run for a count down and such. This was just for my own knowledge and I know there are lots of other and better ways to accomplish what I want in the program, but not learning what I am trying to figure out (user interaction in a while loop).

Bear Bibeault wrote:OK, I'll ask the question one more time, so please think about it a bit:

Why is the loop there in the first place?

Bear Bibeault
Marshal
Posts: 67226
169

John Morgan wrote:I know there are other ways to do this (i.e. just using if statements) however I wanted to work on while loops in javascript and this seemed like a simple project to do the count down.

Herein lies the danger in choosing an approach before even knowing the problem. This isn't a matter of "I know there are other ways" -- it's a matter that a loop cannot be used for this. That's not the way GUIs work.

In a Java console program, you can loop and stall waiting for input from the keyboard. That is not the case with GUIs such as web pages, or even Java GUIs such as Swing or JavaFX.

Such program are event-driven; you don't stall waiting for events, you register handlers that get called when events occur.

The reason that you have an infinite loop is that in response to a single event, you put the code into loop that's expecting to handle multiple events, when it can only handle a single event.

A click event handler handles one click, and one click only.

So, if your aim is to learn event handling, your code is way off the mark. If the goal is to learn loops, user event handling is not where that is going to happen. Choose something like converting an array of Fareheit values to Celcisus or some other data collection manipulation task.

 All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad: RavenDB is an Open Source NoSQL Database thatâ€™s fully transactional (ACID) across your database https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database