0

I have created an array

var array1 = new Array("jack", "kaziu", "wladek");
var display = Math.floor((Math.random() * array1.length));
console.log(display)

I cannot figure out how to display characters instead of numbers (array1's length). Appreciate for help.

adiga
  • 34,372
  • 9
  • 61
  • 83
ledinos1
  • 49
  • 1
  • 6
  • What is your expected output? – Sid Feb 26 '19 at 11:34
  • Can you be more specific? – ellipsis Feb 26 '19 at 11:34
  • Possible duplicate of [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) and [Get random item from JavaScript array](https://stackoverflow.com/questions/5915096) – adiga Feb 26 '19 at 11:34
  • Possible duplicate of [how to how to select random object and the length of the random selected object at the same time](https://stackoverflow.com/questions/54170525/how-to-how-to-select-random-object-and-the-length-of-the-random-selected-object) – Thomas Feb 26 '19 at 11:35
  • 2
    `display` gets a random index. Just use `array1[display]` – adiga Feb 26 '19 at 11:36

1 Answers1

2

Index the array with your calculated index:

const array1 = new Array("jack", "kaziu", "wladek");
const display = array1[Math.floor(Math.random() * array1.length)];

console.log(display);
jo_va
  • 13,504
  • 3
  • 23
  • 47