Percentage chance of saying something?

前端 未结 5 1127
自闭症患者
自闭症患者 2021-01-30 05:09

How do I make it so ..

  • 80% of the time it will say sendMessage(\"hi\");
  • 5 % of the time it will say sendMessage(\"bye\");
  • <
5条回答
  •  借酒劲吻你
    2021-01-30 05:37

    Generate a 20% chance to get "Yupiii!" in the cancel.log

    const testMyChance = () => {
    
      const chance = [1, 0, 0, 0, 0].sort(() => Math.random() - 0.5)[0]
    
      if(chance) console.log("Yupiii!")
      else console.log("Oh my Duck!")
    }
    
    testMyChance()
    

提交回复
热议问题