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

257
Visualizações
Random Fruit guessing Game

I'm in my second week of Javascript and the task we've been given is to create a guessing game.

The steps we've been given are:

  1. Prepare a list of your favourite fruits and store it in an appropriate data structure and have the computer select a random fruit as the secret word.

  2. Base on the selected fruit give the use a hint like the example below. you can use prompt, alert or console.log to show the hint

for example if the secret fruit is "banana"

hint: it's 6 characters long. Starts with b and ends with a. guess the fruit. instead of typing out the hint manually for every fruit. try to use a template string and programmatically work out the starting letter, ending letter and how many characters long.

  1. Allow the user to guess the fruit repeatedly until they guess correctly. keep track of the number of guesses.

  2. Congratulate the user and display number of attempts they made.

I've been sitting on this for awhile and have tried shifting my code around. I know I'm close but would love some pointers or tips.

This is what I have so far:

var fruits = ["kiwi", "banana", "apple", "strawberry", "watermelon", "orange"];

var ranNum = Math.floor(Math.random() * fruits.length);

var secretFruit = fruits[ranNum];

var userPrompt = prompt("Guess the fruit");

var guess = 1;


while (userPrompt !== secretFruit) {

  prompt(
    "hint: it's " +
      secretFruit.length +
      " characters long. Starts with " +
      secretFruit[0] +
      " and ends with " +
      secretFruit.slice(-1) +
      ". guess the fruit."
  );

  guess++;

}
if (userPrompt == secretFruit) {

  alert(
    "Congratulations you guessed the fruit, and it took you " +
      guess +
      " guesses"
  );
}

Any help would be great, thank you.

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

0

Two things wrong: your while loop and hint prompt.

First of all, your while loop will continuously keep prompting without ever checking whether the answer was correct, because you have left the if statement outside of the loop.

Also, the if statement should be before the hint statement otherwise the person will always have to try again even if they got the answer correct.

Secondly, your hint prompt is never actually recorded. This means that despite getting the answer correct, the code never actually records it.

Also, I suggest adding a break statement so that the game ends.

This is a working version of your code:

    var fruits = ["kiwi", "banana", "apple", "strawberry", "watermelon", "orange"];

var ranNum = Math.floor(Math.random() * fruits.length);

var secretFruit = fruits[ranNum];

var userPrompt = prompt("Guess the fruit");

var guess = 1;


while (userPrompt !== secretFruit) {
if (userPrompt == secretFruit) {

  alert(
    "Congratulations you guessed the fruit, and it took you " +
      guess +
      " guesses"
  );break; //break statement here stops infinite loop when correct
}
  userPrompt = prompt(
    "hint: it's " +
      secretFruit.length +
      " characters long. Starts with " +
      secretFruit[0] +
      " and ends with " +
      secretFruit.slice(-1) +
      ". guess the fruit."
  );

  guess++;
}
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