【问题标题】:How to generate random number and print shiny if equals如果等于,如何生成随机数并打印闪亮
【发布时间】:2022-01-20 21:04:09
【问题描述】:

我正在学习编码,目前我被 JavaScript 对象方法所困。如果随机数生成 = 5,我正在尝试编写代码以打印“闪亮”。我该如何实现?此外,它是否打印 undefined ,我不知道它来自哪里。感谢您的阅读。

const pokemonGoCommunityDay = {

  wildEncounters: ['machop','roselia','swablu','gible','snivy','fletchling'],

  currentEncounter() {

    while (this.currentEncounter) {
      this.currentEncounter = this.wildEncounters[Math.floor(Math.random() * 6)];
      console.log(this.currentEncounter);

      if (/* How to make it generate a certain number and if =5 log shiny*/ === 5){
        console.log('Shiny!');
      }break;
    }
  }
}  
console.log(pokemonGoCommunityDay.currentEncounter());

【问题讨论】:

    标签: javascript object math random methods


    【解决方案1】:

    您的代码中已经有了必要的部分 :)

    你可以这样做:

    if (Math.floor(Math.random() * 6)==5){
      console.log('Shiny!');
    }
    

    【讨论】:

    • 感谢您的帮助。我越来越不确定,但不知道它来自哪里。
    猜你喜欢
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2011-05-16
    • 2011-01-25
    相关资源
    最近更新 更多