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

111
Vistas
Returned expression coming up as undefined

I have this simple function I wrote that generates a random number, gets the object in an array at that index and then returns that object

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

The problem is when I try to use joke, it comes up as undefined

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

Could someone please point out my mistake?

This is what the jokes array looks like if it helps

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

you can do something like this

I changed Math.ceil with Math.floor because you want an index between 0 and length - 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