Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why is my console displaying my result as numbers insteadof the actual string in my array in the code below

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

function whosPaying(names) {
  var bigBoss = arrayNames.length;
  var ogaMi = Math.floor((Math.random() * bigBoss));

  return ogaMi + " is paying for the bill"
}

console.log(whosPaying());

>Solution :

The reason is you just get the index,not the element itself

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

so change

 var ogaMi = Math.floor((Math.random() * bigBoss));

to

 var ogaMi = arrayNames[Math.floor((Math.random() * bigBoss))];
var arrayNames = ["Angela", "Ben", "Jenny", "Michael", "Chloe"];

function whosPaying(names) {
  var bigBoss = arrayNames.length;
  var ogaMi = arrayNames[Math.floor((Math.random() * bigBoss))];

  return ogaMi + " is paying for the bill"
}

console.log(whosPaying());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading