Question #180246

  1. Task #2 – Wanna play a game?
  2. We want to create a short program that performs the following task. A user is asked if they would like to play a game. If they answer y, yes, Yes or any other variation of the word (starting with a Y) – we will print to the screen “Hooray! So glad to be playing a game with you!”. If they answer n, no, No or any other variation of the word No (starting with an N), then we will display to the screen “Sorry that you don’t want to play with me :( “. If they enter any other value, we display “Sorry – didn’t get that – please enter your choice again” and they will be prompted to run the program once more. Your code must include the following:
  • toLowerCase function, a loop and the substr function 

Expert's answer

<!DOCTYPE html>
<html>
<head>


    <script>
        var msg = "Hooray! So glad to be playing a game with you!"
        var answer =""


        while (answer.toLowerCase() != "yes" && answer.toLowerCase() != "y" && answer.toLowerCase().substr(0,1) != "y"  &&
               answer.toLowerCase() != "no" && answer.toLowerCase() != "n" && answer.toLowerCase().substr(0, 1) != "n") {
            answer = prompt("Wanna play a game? y,yes,Yes - for yes or  n, no, No for no: ")
        }
        if (answer.toLowerCase() == "no" || answer.toLowerCase() == "n" || answer.toLowerCase().substr(0, 1) == "n") {
            msg = "Sorry that you don’t want to play with me :("
        }
       
        window.onload = function () {
            document.getElementById("message").innerHTML = msg;
        };
    </script>
</head>
<body>
    <p id="message"></p>
</body>
</html>
LATEST TUTORIALS
APPROVED BY CLIENTS