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

166
Vistas
Obtengo resultados de una matriz diferente cuando uso random()

Soy nuevo en la codificación, así que tengo problemas con esto.

Estoy tratando de crear un juego en el que obtienes una cocina al azar y de esa cocina obtienes un restaurante al azar para esa cocina en particular.

Sin embargo, cuando ejecuto el código, a veces termino obteniendo un restaurante con una cocina diferente.

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

0

Almacene el resultado de whatToEat para que lo llame una vez, no en cada instrucción if.

 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 Denunciar

0

Es porque está llamando a su función aleatoria whatToEat dos veces en la matriz 'cuisines', lo que le dará dos resultados diferentes. Almacene el resultado en un enlace para que no cambie:

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