Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!