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

113
Vistas
La expresión devuelta aparece como indefinida

Tengo esta función simple que escribí que genera un número aleatorio, obtiene el objeto en una matriz en ese índice y luego devuelve ese objeto

 function darkHumor(){ let randInt = Math.ceil(Math.random() * jokes.length); let joke = jokes.at(randInt); console.log(randInt); return joke; }

El problema es que cuando trato de usar una joke , aparece como indefinido.

 <div className="buildUpArea">{joke.buildUp}</div>

¿Podría alguien señalar mi error?

Así es como se ve la matriz de chistes si ayuda

 const jokes = [ { id: "1", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "2", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "3", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "4", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "5", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, ] export default jokes
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

puedes hacer algo como esto

Cambié Math.ceil con Math.floor porque quieres un índice entre 0 y longitud - 1

 function darkHumor(jokes){ let randInt = Math.floor(Math.random() * jokes.length); let joke = jokes[randInt]; console.log(randInt); return joke; } const jokes = [ { id: "1", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "2", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "3", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "4", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, { id: "5", buildUp: "test build up", punchLine: "test puncline", jokeInfo: "test joke info", infoLink: "test info link", }, ] Array.from({length: 10}).forEach(() => console.log(darkHumor(jokes)))

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