Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
JavaScript - begginner questions

I have some difficulties with my first number guessing "game" in JavaScript. Can someone have a look and guide me what I did wrong? Started with that language not that long ago..

Function is assigned to one button

<input id="box;" class="btn" ; type="button" value="Guess" onClick="check()">Click to start !

User (player) have to press the button, guess the number (1-10) within 3 attemps and then play again or not. Every attemp I do it says "number is higher" but at the end result is random, even if you chose 10.

var hiddenNum;
var attemps;

hiddenNum = Math.floor(Math.random() * 10);
hiddenNum = hiddenNum + 1;
attemps = 0;


    function check(guess) {
        window.prompt("Please enter the number between 1 and 10", "10");

        if (hiddenNum == guess) {
            window.alert("Congratulations! You guessed correctly !");

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        } else {
            attemps = attemps + 1;

            if (hiddenNum < guess) {
                result = "lower";
            } else {
                result = "higher";
            }

            window.alert("Guess number " + attemps + " is incorrect. The number is " + result + ".");

        }

        if (attemps >= 3) {
            window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum);

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        }

    }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You designed your check function to have a guess passed to it but you are just calling it without doing so. So all your comparisons are against undefined.

You are also using window.prompt to get the actual guess. What you need to do is save the value returned from the prompt into a variable and then compare it. Check the snippet.

var hiddenNum;
var attemps;

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

hiddenNum = hiddenNum + 1;
attemps = 0;

function check() {
  let guess = window.prompt("Please enter the number between 1 and 10", "10");

  if (hiddenNum == guess) {
    window.alert("Congratulations! You guessed correctly !");

    again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

    if (again == "N" || again == "n") {
      window.alert("Thanks for trying. Goodbye.");
      window.close();
    } else {
      window.alert("The number has been randomized.");
      window.location.reload();
    }
  } else {
    attemps = attemps + 1;

    if (hiddenNum < guess) {
      result = "lower";
    } else {
      result = "higher";
    }

    window.alert(
      "Guess number " + attemps + " is incorrect. The number is " + result + "."
    );
  }

  if (attemps >= 3) {
    window.alert(
      "Sorry, you have run out of guesses! The number was " + hiddenNum
    );

    again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

    if (again == "N" || again == "n") {
      window.alert("Thanks for trying. Goodbye.");
      window.close();
    } else {
      window.alert("The number has been randomized.");
      window.location.reload();
    }
  }
}
<input id="box;" class="btn" ; type="button" value="Guess" onClick="check()">Click to start 

about 4 years ago · Juan Pablo Isaza Relatório

0

you need to assign the prompt

var hiddenNum;
var attemps;

hiddenNum = Math.floor(Math.random() * 10);
hiddenNum = hiddenNum + 1;
attemps = 0;



    function check() {
    
        let guess = window.prompt("Please enter the number between 1 and 10", "10");
      
        if (hiddenNum == guess) {
            window.alert("Congratulations! You guessed correctly !");

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        } else {
            attemps = attemps + 1;

            if (hiddenNum < guess) {
                result = "lower";
            } else {
                result = "higher";
            }

            window.alert("Guess number " + attemps + " is incorrect. The number is " + result + ".");

        }

        if (attemps >= 3) {
            window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum);

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        }

    }
<input id="box;" class="btn" ; type="button"  value='guess'onClick="check()">Click to start!
User (player) have to press the button, guess the number (1-10) within 3 attemps and then play again or not. Every attemp I do it says "number is higher" but at the end result is random, even if you chose 10.

about 4 years ago · Juan Pablo Isaza Relatório

0

You should store the value from window.prompt. In your code, you just use guess in the parameter of the function as the value of window.prompt which is incorrect.

Syntax for window.promp:

result = window.prompt(message, default);

Also, I have shorten your codes and store the duplicate item in a function to make it a little bit less messy.

function smallcheck() {
  let again = window.prompt("Would you like to try again? Enter Y or N.", "Y");
  if (again.toUpperCase() == "N") {
    window.alert("Thanks for trying. Goodbye.");
    window.close();
  } else {
    window.alert("The number has been randomized.");
    window.location.reload();
  }
}

function check() {
  let guess = window.prompt("Please enter the number between 1 and 10", "10");
  console.log(guess)
  if (hiddenNum == guess) {
    window.alert("Congratulations! You guessed correctly !");

    smallcheck()

  } else {
    attempts++;

    if (hiddenNum < guess) {
      result = "lower";
    } else {
      result = "higher";
    }
    window.alert("Guess number " + attempts + " is incorrect. The number is " + result + ".");

  }
  if (attempts >= 3) {
    window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum);

    smallcheck()

  }

}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda