Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
Passing an array as a parameter to a function to pick a random string

I am currently doing a javascript course on udemy and ran into a problem with a small coding challenge. The code is supposed to pick a random person from an array. My code works on their test platform, but i cant get it to work in the chrome developer tools.

var names = ["Angela", "Ben", "Jenny", "Michael", "Chloe"];
 
function whosPaying(names) {
    var randomNumber = Math.random();
    var arrayLength = names.length;
    var hasToPay = Math.floor((arrayLength * randomNumber));
 
    return names[hasToPay] + " is going to buy lunch today!"
}
 
whosPaying();

If i write my code like this, it works in the test environment when I submit the code without the array "names", but in chrome I get the following error:

index.js:5 Uncaught TypeError: Cannot read properties of undefined (reading 'length')
    at whosPaying (VM165 index.js:5)
    at VM165 index.js:11

If i define the array inside the function, it works, but that is not really what I'm supposed to do.

Im wondering how i can make the function use the array that is defined outside of the function. In the exercise instructions the array and the way it is passed on to the function are predefined exactly the same way i did it.

I could not find a solution to this that does not include completely different syntax or stuff I am not yet supposed to know at this point in the course.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

over 4 years ago · Santiago Trujillo Relatório

0

Override global names if you pass a parameter with the same name.

 var names = ["Angela", "Ben", "Jenny", "Michael", "Chloe"]; function whosPaying(names) { var randomNumber = Math.random(); var arrayLength = names.length; var hasToPay = Math.floor((arrayLength * randomNumber)); return names[hasToPay] + " is going to buy lunch today!" } let r = whosPaying(); console.log(r);

over 4 years ago · Santiago Trujillo Relatório

0

You can pass parameters to the function and print the return value:

function whosPaying(names)
{
    var randomNumber = Math.random();
    var arrayLength = names.length;
    var hasToPay = Math.floor((arrayLength * randomNumber)); 
    return names[hasToPay] + " is going to buy lunch today!"
}

var names = ["Angela", "Ben", "Jenny", "Michael", "Chloe"];

console.log(whosPaying(names))

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda