如何在javascript中使用随机函数中的参数?

如何在javascript中使用随机函数中的参数?,javascript,arrays,object,Javascript,Arrays,Object,此行无法从首选对象提取随机数据。 //const randomQuote=arr1.quote[Math.floor(Math.random()*quote.length)] 有什么问题吗 功能情感(体裁){ //将其抽象为一个单独的函数 const randomNum=max=>Math.floor(Math.random()*max) const arr1={Happy,Sad,Anger};//不要使用var //了解体裁 const selectedGenre=arr1[类型] //然

此行无法从首选对象提取随机数据。 //const randomQuote=arr1.quote[Math.floor(Math.random()*quote.length)]

有什么问题吗


功能情感(体裁){
//将其抽象为一个单独的函数
const randomNum=max=>Math.floor(Math.random()*max)
const arr1={Happy,Sad,Anger};//不要使用var
//了解体裁
const selectedGenre=arr1[类型]
//然后随机引用它
const randomQuote=selectedGenre[randomNum(randomGene.length)];
//作为console.log()返回不是一种好的做法
//这将影响函数的可重用性
返回随机报价;
}
console.log(情绪('Happy'))

Satyam大家好,欢迎来到Stackoverflow。你的要求并不完全正确。此外,您的格式设置还需要一些工作。请阅读并编辑您的问题。
function Emotions(var quote) {
    var Happy = [
         "happiness is when what you think, what you say, and what you do are in harmony.",
         "There is only one happiness in this life, to love and be loved.",
         "Be happy for this moment. This moment is your life.",
         "Happiness lies in the joy of achievement and the thrill of creative effort.",
         "Be kind whenever possible. It is always possible.",
         "Adopt the pace of nature: Her secret is patience.",
         "Spread love everywhere you go. Let no one ever come to you without leaving happier.",
         "Resolve to keep happy, and your joy and you shall form an invincible host against difficulties.",
         "The present moment is filled with joy and happiness. If you are attentive, you will see it.",
         "Action may not always bring happiness, but there is no happiness without action."];

var Sad = [
     "It’s sad when you realize you aren’t as important to someone as you thought you were",
     "What do you do when the only one who can make you stop crying is the one who made you cry",
     "I smile not for that I am happy, but sometimes I smile to hide sadness.",
     "Sometimes, crying is the only way your eyes speak when your mouth can’t explain how broken your heart is.",
     "The ones that you love the most are usually the ones that hurt you the most! ",
     "Ignore me. I don’t care. I’m used to it anyways. I’m invisible.",
     "I’m not okay, I’m just good at pretending I am.",
     "Nothings worse, is to see them two together, knowing I will never have him again.",
     "It hurts the worst when the person that made you feel so special yesterday, makes you feel so unwanted today.",
     "Sometimes it’s better to be Alone…Nobody can hurt you."
];

var Anger = ["I shall allow no man to belittle my soul by making me hate him",
             "Anger and intolerance are the enemies of correct understanding.",
             "When anger rises, think of the consequences.",
             "For every minute you remain angry, you give up sixty seconds of peace of mind.",
             "Never go to bed mad. Stay up and fight.",
            "Angry people are not always wise",
            "The best fighter is never angry",
            "Speak when you are angry and you will make the best speech you will ever regret.",
            "Anger, resentment and jealousy doesn't change the heart of others-- it only changes yours.",
            "A heart filled with anger has no room for love"]

var arr1 = { Happy, Sad, Anger};
const randomQuote = arr1.quote[Math.floor(Math.random() * quote.length )];
return console.log(randomQuote);
}

Emotions(`Fear`);