• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Infinite Loop problem  RSS feed

 
Ranch Hand
Posts: 147
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 147
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
John Morgan
Ranch Hand
Posts: 147
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!