简体   繁体   English

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

[英]How to use Arguements in random Function in javascript?

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

this line not working in extracting the random data from preferrred objects.此行无法从首选对象中提取随机数据。 // const randomQuote = arr1.quote[Math.floor(Math.random() * quote.length )]; // const randomQuote = arr1.quote[Math.floor(Math.random() * quote.length )];

What's the problem?有什么问题?


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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM