6

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.

1
  • 1
    Just send the argument whosPaying(names); Commented Jan 4, 2022 at 20:21

5 Answers 5

4

you've called whosPaying without any args, then names end up being undefined.

Do this instead: whosPaying(names), or drop the names parameter of whosPaying

Sign up to request clarification or add additional context in comments.

2 Comments

@TheHansDampf FWIW the easiest thing to do is put a console.log (or breakpoint) immediately inside the function to check your assumptions around the the parameter(s). Once you saw that it was undefined the logical place to look at where it's being called.
In this case, isn't this better to be written without argument & parameter? Or if be both is better?
2

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

var names = ["Angela", "Ben", "Jenny", "Michael", "Chloe"];
 
function whosPaying() {    
    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);

Comments

2

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))

Comments

1

As it has been pointed out that you didn't provide the names argument when you invoked your whosPaying() function... because you used var to declare the names variables, you can do this without passing any arguemnt because of scoping.

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

Also this...

var randomNumber = Math.random();
var arrayLength = names.length;
var hasToPay = Math.floor((arrayLength * randomNumber));

Can be converted to this:

var hasToPay = Math.floor(Math.random() * names.length);

Which is a single line instead of three.

Comments

0

I assume that you want to pick a random index from your array , you can try this

function generateRandom(Array) {
  let random = Math.floor(Math.random() * Array.length)
  return Array[random]
}

1 Comment

Please don't, however, name a parameter after an existing thing (and better to stick to standard JS naming conventions).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.