Home > Back-end >  Call function to display array random list of name
Call function to display array random list of name

Time:07-22

code image

When I call the function, it must display A random person's name.

CodePudding user response:

You should correct names.length to listof names.length for example enter image description here

CodePudding user response:

// You only add a paramter when you provide an argument when calling the function, for example if the listOfNames array was outside the function
function whosPaying() {
    let listOfNames = ["Mohamed", "Ahmed", "Farah", "Yusuf", "Dahir"];
    let numberOfPeople = Math.floor(Math.random() * listOfNames.length);

    let randomPerson = listOfNames[numberOfPeople];
    return randomPerson   " is going to buy lunch today!";
}
console.log(whosPaying());
  • Related