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

标签 javascript arrays object

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`);

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

最佳答案

有什么问题吗?


function Emotions(genre) {
  // Abstracting this as a separate function
  const randomNum = max => Math.floor(Math.random() * max)


  const arr1 = { Happy, Sad, Anger}; // Don't use var

  // Get the Genre
  const selectedGenre = arr1[genre]

  // Then random Quote from it
  const randomQuote = selectedGenre[randomNum(randomGenre.length)];

  // Returning as console.log() is not good practice
  // it will affect the function's reusability
  return randomQuote;
}

console.log(Emotions('Happy'))

关于javascript - 如何在javascript中的随机函数中使用参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60811779/

相关文章:

c++ - 将数组中的元素设置为 0 或 NULL

Javascript如何知道一个数组是否是另一个数组的子数组

javascript - 如何在 Firebase 表上进行连接

javascript - 遍历列表并插入对象数组

javascript - 等待,文件准备就绪

python - 在python中遍历二维数组并将项目添加到字典时遇到问题

javascript - 在 Azure Media Player 上插入按钮

python - 实现在属性更新时调用外部函数的 Python 类的正确方法

pandas 将带有数字和 nans 的对象转换为整数或 float

c++ - 如何正确地 std::bind 一个类函数?