How to output random JS variables

I have a lot of different fruits. Orange, Banana, Apples... I need JS to randomly give each person a fruit. The code looks like this:

var name, fruit1, fruit2, fruit3

fruit1 = ("Апельсин");
fruit2 = ("Банан");
fruit3 = ("Яблоки");

name = prompt("Введите Ваше имя")
document.write(name+" "+RandomFruit)

How can I make RandomFruit substitute a random fruit instead of ?

Author: Yurii Space, 2018-02-07

2 answers

If I understand you correctly, then something like this. There is, of course, a little perversion) But where without it XD

var name, fruits, number;

fruits = [
    'Апельсин',
  'Банан',
  'Яблоки',
  'Апельсин',
  'Любой фрукт'
];

function getRandom(min, max) {
  return Math.random() * (max - min) + min;
}
number = getRandom(0, fruits.length);

name = prompt("Введите Ваше имя");


document.write(name +" "+ fruits[parseInt(number)]);
 2
Author: Taarim, 2018-02-07 22:38:41
Array.prototype.randomItem = function() {
  return this[Math.floor(Math.random()*this.length)];
}

const fruits = [
  'Апельсин',
  'Банан',
  'Яблоко'
];

console.log(fruits.randomItem());
 0
Author: MoloF, 2020-06-13 10:36:28