Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:29:54+00:00 2026-05-29T07:29:54+00:00

One of my friends is teaching a programming class with Javascript and one of

  • 0

One of my friends is teaching a programming class with Javascript and one of his assignments was to create a number guessing game. This was his example implementation:

funProgram: for(;;) {
  numberGuesser: {
    var num = (Math.random() * 100) | 0;
    var guess = +prompt("I'm thinking of a number between 0 and 100. Try to guess it.", 0);
    var guesses = 1;
    guess: for(;;) {
      higher: {
        lower: {
          if(guess === num) break guess;
          if(guess > num) break lower;
          guess = +prompt("Too low. Try again.", 0);
          break higher;
        }
        guess = +prompt("Too high. Try again.", 0);
      }
      guesses++;
    }
    alert("You got it in " + guesses + " guesses! The number is " + num);
  }
  var again = prompt("Do you want to guess again (y/n)?", "y") === "y";
  if(!again) break funProgram;
}

He told me that it’s a good practice to label your code and wrap blocks around it so you can easily see what each section is doing. He also said labeled breaks and continues are much easier to read than unlabeled ones because you can know exactly what you are breaking out of. I’ve never seen any code patterns like this, so I’m not sure if this is true.

I’ve been using Javascript for a while and there are a few things in here that I’ve never seen before and some things that I still don’t understand. I thought that the break keyword was specifically meant for breaking out of loops. The higher and lower blocks are not loops, but apparently you can still break out of it. How is that possible? It seems odd to me to break out of something that doesn’t loop. Can you also break out of functions using the break keyword?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:29:56+00:00Added an answer on May 29, 2026 at 7:29 am

    Breaks can in fact have labels (and do so accept them). However, I’m not sure who “He” is, but I would go as far as to say “He” is conveying his ideals of programming, rather than a specific standard. That is to say, there’s no need to use labels, it just makes it more legible to that particular person. (And, IMHO, labels are reminiscent of the BASIC/GOTO days which usually results in Spaghetti Code).

    Extra Credit: Ask your friend if they used to write in BASIC; I’m betting you’ll get a “yes” (along with a lot of bad habits for the duration of the course–that’s not to profile, I’ve just never had a good experience with BASIC/VB programmers following [current] coding patterns))

    The break command is commonly used to exit loops, however if the code block is nested within a label, you’re establishing yet another block which you can break from. It also gives you a bit more flexibility as to where the break is meant to exit from. For example:

    for (;;){ // "for a"
      for(;;){ // "for b"
        break; // breaks "for b"
      }
    }
    

    In this instance, break is only meant to exit the nested (“for b”) loop. However:

    myblock: {
      for(;;){
        for(;;){
          break mybock; // breaks label "myblock"
        }
      }
    }
    

    In this case, break is actually exiting both loops because you’re instructing it to quit the label block entirely. This would be almost like having:

    function myblock(){
      for(;;){
        for(;;){
          return; // exits function "myblock"
        }
      }
    }
    

    Where return exits the block similar to the way break myblock acts.


    By the way, not for nothing, I find this a tad easier to read:

    var again = true;
    while (again){
        var num = (new Date()).getMilliseconds() % 100,
            guess = +prompt("I'm thinking of a number between 0 and 100. Try to guess it.", "1"),
            guesses = 1;
        while (num !== guess){
            guesses++;
            guess = +prompt((guess < num ? "Too low." : "Too high.") + " Try again.", guess);
        }
        alert("You got it in " + guesses + " guesses! The number is " + num);
        again = prompt("Do you want to guess again? (y/n)", "y") == "y";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of my friends faced this in his recent interview. I'm posting 2 questions
My Friends, I spent quite some time on this one... but cannot yet figure
i have got this question to solve from one my friends. i have searched
For example Facebook redirects if you click on a link one of you friends
I'd like to have a game in which everytime one of your friends beats
Dear Friends good afternoon. My problem may be this is very basic one i.e.
One of my friends raised this problem to me. I was stuck because I
This is a debate I was having with one of my friends: What would
One of my friends helped me make an android app and he used his
According to my last post in here one of friends suggested this code: using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.