JavaScript How to end program after right answer? phd program right after undergraduate,mba program right after undergrad,

I've made a game with JavaScript where a player can only guess a random number from 1 to 10 three times, each time the program reads a wrong answer it displays what should've been the right answer, and tells the player to try again, if the player gets the right answer the game displays a message saying You got it right. I've managed to make the program work, and apparently, everything seems to be fine, except for one thing, the program won't stop even after the player gets the right answer, it reads through all the statements until it reaches the end. How can I make it stop after the right answer?

var random = Math.floor(Math.random() * 10);

var random1 = Math.floor(Math.random() * 10);

var random2 = Math.floor(Math.random() * 10);

var answer = window.prompt("Make a guess from 1 to 10, you have 3 chances.");
if (answer == random) {
  alert("HORAAYYYY YOU GOT IT RIGHT!!!");
} else {
  alert("Sorry, the correct answer was " + random);
  window.prompt("Make a guess from 1 to 10, you have 2 chances left.");
}

if (answer == random1) {
  alert("HORAAYYYY YOU GOT IT RIGHT!!!");
} else {
  alert("Sorry, the correct answer was " + random1);
  window.prompt("Make a guess from 1 to 10, you have 1 chance left.");
}

if (answer == random2) {
  alert("HORAAYYYY YOU GOT IT RIGHT!!!");
} else {
  alert("Sorry, the correct answer was " + random2);
  alert("You've lost");
}
Answer:1

First and forement, the bug

You have a bug in your code. If you win on the first round, it will detect this successfully. On the second and third round, any victory will be purely coincidence.

The reason is because you do not assign the return value from window.prompt to answer every time you call it. So the value of answer never changes from one round to the next.

To fix this, you should replace

window.prompt(...)

with:

answer = window.prompt(...)

The "bad" fix:

Let's start with a very simple (but very bad) solution, and use it as a springboard to teaching better architecture design.

Your current code roughly looks like this:

if (win) {
   // say you won
} else {
   // say you lost
}



if (win) {
   // say you won
} else {
   // say you lost
}



if (win) {
   // say you won
} else {
   // say you lost
}

With all of the extra stuff cleaned up you can clearly see why it's going through all three iterations: the three if/else blocks are entirely unrelated and know nothing about one another. It runs one if/else block, then another, then another -- in order, every time.

The easiest fix is to make sure the later blocks only run if you lose. This is pretty easy to do, because we already know if you lost -- it happens when you didn't win!

if (win) {
   // say you won
} else {
   // say you lost
   if (win) {
      // say you won
   } else {
      // say you lost
      if (win) {
         // say you won
      } else {
         // say you lost
      }
   }
}

Or, using your random, random1, and random2 variables:

var random = Math.floor(Math.random() * 10);
var random1 = Math.floor(Math.random() * 10);
var random2 = Math.floor(Math.random() * 10);

var answer = window.prompt("Make a guess from 1 to 10, you have 3 chances."); 
if (answer == random) {
    alert("HORAAYYYY YOU GOT IT RIGHT!!!"); 
}
else {
    alert("Sorry, the correct answer was " + random);
    answer = window.prompt("Make a guess from 1 to 10, you have 2 chances left.");
    if (answer == random1) {
        alert("HORAAYYYY YOU GOT IT RIGHT!!!"); 
    }
    else {
        alert("Sorry, the correct answer was " + random1);
        answer = window.prompt("Make a guess from 1 to 10, you have 1 chance left.");
        if (answer == random2) {
            alert("HORAAYYYY YOU GOT IT RIGHT!!!"); 
        }
        else {
            alert("Sorry, the correct answer was " + random2);
            alert("You've lost");
        }
    }
}

This is ugly, but will work.

Springboarding into better design

As you might imagine, adding 4 or 5 or 6 rounds to this game would get REALLY tedious. You'd have to type out even more if/else blocks, create even more random variables, and type out even more alert statements. To make it more annoying, all of these alert statements contain the same text!

There's a concept in software design called DRY (Don't Repeat Yourself). This means that if you have two identical lines of code, you can probably rewrite it to eliminate the duplication.

In your case, we can do this using a while loop to check if we've won the game or not:

var youHaveWon = false;

while( ! youHaveWon ) {
    var random = Math.floor(Math.random() * 10);
    var answer = window.prompt("Make a guess from 1 to 10");
    if (answer == random) {
        alert("HORAAYYYY YOU GOT IT RIGHT!!!"); 
        youHaveWon = true;
    }
    else {
        alert("Sorry, the correct answer was " + random);
    }
}

This will allow you to keep making guesses until you get it right, and doesn't repeat any code. Although this doesn't limit you to only 3 guesses. To do that, we should introduce one more variable:

var youHaveWon = false;
var guessesRemaining = 3;

while( ! youHaveWon && guessesRemaining > 0 ) {
    var random = Math.floor(Math.random() * 10);
    var answer = window.prompt("Make a guess from 1 to 10... you have " + guessesRemaining + " more guesses");
    if (answer == random) {
        alert("HORAAYYYY YOU GOT IT RIGHT!!!"); 
        youHaveWon = true;
    }
    else {
        alert("Sorry, the correct answer was " + random);
    }
    guessesRemaining = guessesRemaining - 1;
}

if ( ! youHaveWon ) {
    alert("You lost");
}
Answer:2

You can wrap this to a function and return it on right value. Upon return, JS will stop execution and control will move forward

Try below

function findNumber() {

  var random = Math.floor(Math.random() * 10);

  var random1 = Math.floor(Math.random() * 10);

  var random2 = Math.floor(Math.random() * 10);

  var answer = window.prompt("Make a guess from 1 to 10, you have 3 chances.");
  if (answer === random) {
    alert("HORAAYYYY YOU GOT IT RIGHT!!!");
    return true;
  } else {
    alert("Sorry, the correct answer was " + random);
    window.prompt("Make a guess from 1 to 10, you have 2 chances left.");
  }

  if (answer == random1) {
    alert("HORAAYYYY YOU GOT IT RIGHT!!!");
    return true;
  } else {
    alert("Sorry, the correct answer was " + random1);
    window.prompt("Make a guess from 1 to 10, you have 1 chance left.");
  }

  if (answer == random2) {
    alert("HORAAYYYY YOU GOT IT RIGHT!!!");
    return true;
  } else {
    alert("Sorry, the correct answer was " + random2);
    alert("You've lost");
  }
}
Answer:3

The problem is that you're having three different if-blocks which all will be executed even though the user might have guessed the correct number yet. I'd recommend setting up a single random number and a global counter which keeps track of the remaining chances.

function validate() {
  if (answer == random) {
    alert("HORAAYYYY YOU GOT IT RIGHT!!!");
  } else {
    if (chances - 1 > 0) {
      chances--;
      answer = window.prompt("Make a guess from 1 to 10, you have " + chances + " chances.");
      validate();
    }
  }
}

var random = Math.floor(Math.random() * 10);
var chances = 3;
var answer = window.prompt("Make a guess from 1 to 10, you have " + chances + " chances.");
validate();
Answer:4

In my site I have a switch between a down arrow & and an up arrow. See here How do I change this to a CSS transition, so there's a brief break between the switch? Here's my code: function ...

In my site I have a switch between a down arrow & and an up arrow. See here How do I change this to a CSS transition, so there's a brief break between the switch? Here's my code: function ...

When hitting https://login.salesforce.com/services/oauth2/token, using Postman. POST https://login.salesforce.com/services/oauth2/token Response: I receive a token, but when I try to do something ...

When hitting https://login.salesforce.com/services/oauth2/token, using Postman. POST https://login.salesforce.com/services/oauth2/token Response: I receive a token, but when I try to do something ...

I'm using DateFNS and I need to generate a countdown with it. distanceInWordsToNow only outputs in about 3 years but I need the exact time like 3 Years, 11 Months, 20 Days, 3 Hours, 2 Minutes. How to ...

I'm using DateFNS and I need to generate a countdown with it. distanceInWordsToNow only outputs in about 3 years but I need the exact time like 3 Years, 11 Months, 20 Days, 3 Hours, 2 Minutes. How to ...

  1. countdown with date
  2. countdown date and time
  3. countdown date in excel
  4. countdown date and time timer
  5. countdown date on iphone
  6. countdown date for desktop
  7. countdown date in javascript
  8. countdown date in powerpoint
  9. countdown date in php
  10. countdown date in android
  11. countdown to date with picture
  12. countdown to date with months
  13. countdown to date app
  14. countdown to date calculator
  15. countdown to date timer
  16. countdown to date online
  17. countdown to date clock
  18. countdown to date widget
  19. countdown to date without weekends
  20. countdown to date html

I am still getting to grips with jQuery and wondered if anyone had a suggestion for this? Basically on click I want to show the div with the matching class, so if you click the btn with class '....

I am still getting to grips with jQuery and wondered if anyone had a suggestion for this? Basically on click I want to show the div with the matching class, so if you click the btn with class '....

  1. show hide with animation
  2. show hide with css
  3. show hide with animation css
  4. show hide with transition css
  5. show hide with animation android
  6. show hide with effect jquery
  7. show hide with checkbox jquery
  8. show hide with fade jquery
  9. show hide with css only
  10. show hide with radio buttons javascript
  11. show hide with animation angular 6
  12. show hide with toggle jquery
  13. show hide with bootstrap
  14. show hide with transition jquery
  15. show hide with js
  16. show hide with radio button
  17. show hide with checkbox
  18. show hide with angular
  19. show hide with php
  20. hide show with transition