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

155
Views
I am getting output from a different array when using random()

I am new to coding so I'm having trouble with this.

I am trying to create a game where you get a random cuisine and from that cuisine you get a random restaurant for that particular cuisine.

However, when I run the code, I end up getting a restaurant for a different cuisine sometimes.

let whereToEat = prompt("Shall we go out to eat today");
let choice;
let cuisines = ["Italian", "Korean", "Thai", "Mediterranean", "Turkish"];
let italianRestaurant = ["Vapiano", "Bella Italia", "Grato"];
let koreanRestaurant = ["Superstar bbq", "Bari Bari", "Korean bbq and vegan"];
let thaiRestaurant = ["Thai pot", "Wok n Roll", "Farang"];
let mediterraneanRestaurant = ["Mediterrnean cafe and restaurant", "Seabird", "Nopi"];
let turkishRestaurant = ["Gokyuzu", "Antalya restaurant", "La'De kitchen"];


function whatToEat(choices) {
  let index = Math.floor(Math.random() * choices.length);
  return choices[index];
};

if (whereToEat === "yes") {
  alert("Great");
  alert("Lets try " + whatToEat(cuisines));
  if (whatToEat(cuisines) === "Italian") {
    alert("Let's go here: " + whatToEat(italianRestaurant));
  } else if (whatToEat(cuisines) === "Korean") {
    alert("Let's go here: " + whatToEat(koreanRestaurant));
  } else if (whatToEat(cuisines) === "Thai") {
    alert("Let's go here: " + whatToEat(thaiRestaurant));
  } else if (whatToEat(cuisines) === "Mediterranean") {
    alert("Let's go here: " + whatToEat(mediterraneanRestaurant));
  } else if (whatToEat(cuisines) === "Turkish") {
    alert("Let's go here: " + whatToEat(turkishRestaurant));
  }
} else {
  alert("Ahh I was hoping you would say yes");
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Store the result of whatToEat so your calling it once not in each if statement.

let whereToEat = prompt("Shall we go out to eat today");
let cuisines = ["Italian", "Korean", "Thai", "Mediterranean", "Turkish"];
let italianRestaurant = ["Vapiano", "Bella Italia", "Grato"];
let koreanRestaurant = ["Superstar bbq", "Bari Bari", "Korean bbq and vegan"];
let thaiRestaurant = ["Thai pot", "Wok n Roll", "Farang"];
let mediterraneanRestaurant = ["Mediterrnean cafe and restaurant", "Seabird", "Nopi"];
let turkishRestaurant = ["Gokyuzu", "Antalya restaurant", "La'De kitchen"];


function whatToEat(choices) {
  let index = Math.floor(Math.random() * choices.length);
  return choices[index];
};

if (whereToEat === "yes") {
  const choice = whatToEat(cuisines);
  alert("Great");
  alert("Lets try " + choice);
  if (choice === "Italian") {
    alert("Let's go here: " + whatToEat(italianRestaurant));
  } else if (choice === "Korean") {
    alert("Let's go here: " + whatToEat(koreanRestaurant));
  } else if (choice === "Thai") {
    alert("Let's go here: " + whatToEat(thaiRestaurant));
  } else if (choice === "Mediterranean") {
    alert("Let's go here: " + whatToEat(mediterraneanRestaurant));
  } else if (choice === "Turkish") {
    alert("Let's go here: " + whatToEat(turkishRestaurant));
  }
} else {
  alert("Ahh I was hoping you would say yes");
}

about 4 years ago · Juan Pablo Isaza Report

0

It's because you're calling your random whatToEat function twice on the 'cuisines' array, which will give you two different results. Store the result in a binding so it doesn't change:

const choice = whatToEat(cuisines);
alert("Lets try " + choice);
if (choice === "Italian") { ... etc
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!