【问题标题】:JavaScript - Random numbers and variables between functionsJavaScript - 函数之间的随机数和变量
【发布时间】:2017-11-20 06:28:53
【问题描述】:

我是 JavaScript 新手,我有两个滚动函数用于每一帧的滚动。我无法将每个滚动的值放入框架函数中以调用和使用。如果有人可以提供帮助,那就太好了!在此先感谢,我的代码如下。

var Bowling = function() {
  var STARTING_TOTAL = 0;
  ROLL_ONE = Math.floor(Math.random() * 11);
  ROLL_TWO = Math.floor(Math.random() * 11);
  this.score = STARTING_TOTAL;
  var firstScore;
  var secondScore;
  var totalScore;

  Bowling.prototype.firstRoll = function() {
     firstScore = ROLL_ONE
     return firstScore;
  };

  Bowling.prototype.secondRoll = function() {
     secondScore =  Math.floor(Math.random() * 11 - firstScore);
     return secondScore;

  };

  Bowling.prototype.frameScore = function () {
     totalScore = firstScore + secondScore
    return totalScore;
  };

};

【问题讨论】:

  • 你永远不会打电话给firstRollsecondRoll。这些是功能。他们需要被调用。一旦你调用它们,返回值然后给你将是你正在寻找的卷。
  • 你想如何使用Bowling?作为一个班级,我猜?你在哪里调用这些方法?
  • 另外,这些功能的意义何在?他们所做的只是将一个成员变量更改为另一个成员变量。
  • 对不起,我有点困惑,学习 JavaScript 一周。是的,将 Bowling 用作一个类,然后将这些方法用于我的程序的不同部分,以便与 html 集成以用于项目的小型 Web 应用程序。
  • @Carcigenicate 你是什么意思我没有调用 firstRoll 和 secondRoll?在我的 frameScore 中?如果不是,我该如何保持生成相同的随机数?谢谢

标签: javascript function variables


【解决方案1】:

为了解决为什么第二次滚动可能是负数的问题:按照您的代码目前的情况,如果第二次滚动小于第一次滚动,则结果将为负数。如果你想要这样,如果第一卷是 6,第二卷将是 0 到 4 之间的数字,请尝试类似:

function randomInt(maxNum) {
    return Math.floor(Math.random() * maxNum)
}
    
var maxRoll = 11
    
var rollOne = randomInt(maxRoll)
var rollTwo = randomInt(maxRoll - rollOne)
    
console.log(rollOne)
console.log(rollTwo)

一遍又一遍地按“运行代码片段”以查看它的工作情况。

我所做的更改:

  • 我做了一个函数,randomInt,它给出了一个从 0 到某个最大数的随机数。这使您无需编写两次相同的代码。

  • 我创建了一个变量maxRoll,用于跟踪可能的最高点数。

  • 我从第一卷中减去 maxRoll 以确定第二卷的最大数量应该是多少 (maxRoll - rollOne)。然后将其提供给randomInt

【讨论】:

    【解决方案2】:

    我只能猜测您要达到的目标。我稍微重构了你的代码:

    var Bowling = function () {
      var STARTING_TOTAL = 0;
      this.score = STARTING_TOTAL; // remains unused; did you mean "totalScore"?
      this.firstScore = 0;
      this.secondScore = 0;
    };
    
    Bowling.prototype.firstRoll = function () {
      this.firstScore = Math.floor(Math.random() * 11);
      return this.firstScore;
    };
    
    Bowling.prototype.secondRoll = function () {
      this.secondScore = Math.floor(Math.random() * 11 - this.firstScore);
      return this.secondScore;
    
    };
    
    Bowling.prototype.frameScore = function () {
      this.totalScore = this.firstScore + this.secondScore
      return this.totalScore;
    };
    
    // now use it like this:
    var bowling = new Bowling();
    console.log(bowling.firstRoll());
    console.log(bowling.secondRoll());
    console.log(bowling.frameScore());

    然而,在我的方法中,firstScoresecondScore 是公共属性。

    【讨论】:

    • 首先@PeterMader 感谢您的帮助。当我在您的代码上调用 firstRoll 时,它告诉我它不是一个函数? (我很抱歉,我确信我没有像我希望的那样清楚)
    • 我使用 StartingTotal 作为我的起始分数 0 @PeterMader。
    • 写“未使用”时,我的意思是score 属性,我猜scoretotalScore 是一样的。并且错误:在 StackOverflow sn-p 中它可以工作。您是否像我一样使用Bowling 类?还是你这样称呼它:Bowling.firstRoll()(与bowling.firstRoll()不同)?
    • 发生这种情况:D
    猜你喜欢
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2012-04-25
    相关资源
    最近更新 更多