Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda