Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How to control the do / while infinite loop?

How to control the do / while infinite loop?

Scheduled Pinned Locked Moved JavaScript
javascripthelptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Ted K 1
    wrote on last edited by
    #1

    Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!

    var target = prompt("Please enter a number small than 50:");

        var guess = "";
        var time = 0;
    
        /\*
        var guess2 = prompt("111");
        var guess3 = window.prompt("222",guess3);
        document.write(typeof(guess));
        document.write(typeof(guess2));
        document.write(typeof(guess3));
        \*/
    
        do{
            guess = prompt("Please Guess:");
            if(guess == target || guess == null){
                break;
            }
    
            if(guess < target){
                alert("bigger!");
                time++;
                continue;
            }else{
                alert("smaller!");
                time++;
                continue;
            }
        }while(true)
    
        if(guess==null){
            document.write("the answer is:"+target);
        }else{
            document.write("Good!共猜了:"+(time+1));
        }
    
    W B W 3 Replies Last reply
    0
    • T Ted K 1

      Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!

      var target = prompt("Please enter a number small than 50:");

          var guess = "";
          var time = 0;
      
          /\*
          var guess2 = prompt("111");
          var guess3 = window.prompt("222",guess3);
          document.write(typeof(guess));
          document.write(typeof(guess2));
          document.write(typeof(guess3));
          \*/
      
          do{
              guess = prompt("Please Guess:");
              if(guess == target || guess == null){
                  break;
              }
      
              if(guess < target){
                  alert("bigger!");
                  time++;
                  continue;
              }else{
                  alert("smaller!");
                  time++;
                  continue;
              }
          }while(true)
      
          if(guess==null){
              document.write("the answer is:"+target);
          }else{
              document.write("Good!共猜了:"+(time+1));
          }
      
      W Offline
      W Offline
      Wombaticus
      wrote on last edited by
      #2

      Try using the isNaN[^] function - i.e.:

      isNaN(guess)

      rather than

      guess == null

      T 1 Reply Last reply
      0
      • T Ted K 1

        Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!

        var target = prompt("Please enter a number small than 50:");

            var guess = "";
            var time = 0;
        
            /\*
            var guess2 = prompt("111");
            var guess3 = window.prompt("222",guess3);
            document.write(typeof(guess));
            document.write(typeof(guess2));
            document.write(typeof(guess3));
            \*/
        
            do{
                guess = prompt("Please Guess:");
                if(guess == target || guess == null){
                    break;
                }
        
                if(guess < target){
                    alert("bigger!");
                    time++;
                    continue;
                }else{
                    alert("smaller!");
                    time++;
                    continue;
                }
            }while(true)
        
            if(guess==null){
                document.write("the answer is:"+target);
            }else{
                document.write("Good!共猜了:"+(time+1));
            }
        
        B Offline
        B Offline
        Blikkies
        wrote on last edited by
        #3

        guess can return an empty string when you click on Ok without entering anything, guess will only be null if you click on cancel or if you close the prompt. Add guess == '' to your if statement or replace guess == null with isNaN(guess) that will check if the value is a legal number

        T 1 Reply Last reply
        0
        • T Ted K 1

          Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!

          var target = prompt("Please enter a number small than 50:");

              var guess = "";
              var time = 0;
          
              /\*
              var guess2 = prompt("111");
              var guess3 = window.prompt("222",guess3);
              document.write(typeof(guess));
              document.write(typeof(guess2));
              document.write(typeof(guess3));
              \*/
          
              do{
                  guess = prompt("Please Guess:");
                  if(guess == target || guess == null){
                      break;
                  }
          
                  if(guess < target){
                      alert("bigger!");
                      time++;
                      continue;
                  }else{
                      alert("smaller!");
                      time++;
                      continue;
                  }
              }while(true)
          
              if(guess==null){
                  document.write("the answer is:"+target);
              }else{
                  document.write("Good!共猜了:"+(time+1));
              }
          
          W Offline
          W Offline
          W Balboos GHB
          wrote on last edited by
          #4

          A few things that might be helpful. You don't really need to test for the 'break' options (as your code appears) if you do something like this:

          do {
          if(something is true) {
          do-this;
          }
          else
          if(something else is true) {
          do this;
          }
          . . . etc . . .
          else // The only condition left
          break;
          // You don't need your "continue;" statements, which are implicit in your loop.

          } while(true)

          This style will handle anything 'unexpected' - that is, anything you didn't intend

          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

          "As far as we know, our computer has never had an undetected error." - Weisert

          "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

          T 1 Reply Last reply
          0
          • W W Balboos GHB

            A few things that might be helpful. You don't really need to test for the 'break' options (as your code appears) if you do something like this:

            do {
            if(something is true) {
            do-this;
            }
            else
            if(something else is true) {
            do this;
            }
            . . . etc . . .
            else // The only condition left
            break;
            // You don't need your "continue;" statements, which are implicit in your loop.

            } while(true)

            This style will handle anything 'unexpected' - that is, anything you didn't intend

            "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

            "As far as we know, our computer has never had an undetected error." - Weisert

            "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

            T Offline
            T Offline
            Ted K 1
            wrote on last edited by
            #5

            :) really useful! thanks a lot!

            1 Reply Last reply
            0
            • W Wombaticus

              Try using the isNaN[^] function - i.e.:

              isNaN(guess)

              rather than

              guess == null

              T Offline
              T Offline
              Ted K 1
              wrote on last edited by
              #6

              :) really useful! thanks a lot!

              1 Reply Last reply
              0
              • B Blikkies

                guess can return an empty string when you click on Ok without entering anything, guess will only be null if you click on cancel or if you close the prompt. Add guess == '' to your if statement or replace guess == null with isNaN(guess) that will check if the value is a legal number

                T Offline
                T Offline
                Ted K 1
                wrote on last edited by
                #7

                ;) really useful! thanks a lot!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups