-4

问题

以下代码编译时出现以下错误:

  • Unexpected token ) path/file.js:3:296

代码

下面的脚本代表一个简单的函数,它接受两个 int 参数,并以某种方式转换它们。我不会详细介绍,因为问题是语法错误。PS。我知道这个算法还不起作用,问题纯粹是错误chrome出现了。

function freeTimeCalculator (fuelPrimary, fuelSecondary) {
var freeTime, secondaryFuel, freeTimeFactor = 0.5, fuelSecondaryFactor = 0.3;

  function displayResults() {
    // Calculate variables
    calculateTime();

    // Display results
    console.log("Free time: " + freeTime);
    return console.log("Secondary fuel: " + secondaryFuel);
  }

  function calculateTime () {
    var hours = [], minutes = [];
        // Notice: both arrays and indexes getting tied up
        hours[0] = minutes[0] = fuelPrimary.toString();
        hours[1] = minutes[1] = fuelSecondary.toString();

    // Calculation on strings
    // Notice: we take advantage that a notation may contain
    // many hours even 100, but the minutes will be always
    // the last two numbers.
    hours[0] = parseInt(hours[0].substr(0, hours[0].length-2));
    minutes[0] = parseInt(hours[0].substr(hours[0].length-2, hours[0].length));
    hours[1] = parseInt(hours[1].substr(0, hours[1].length-2));
    minutes[1] = parseInt(hours[1].substr(hours[1].length-2, hours[1].length));

    // Assigning values to the global variables
    freeTime = ((hours[0] * 60 + minutes[0]) * freeTimeFactor);
    return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
    }
  }
}
4

3 回答 3

2

)在最后一行有一个流浪者。这 -

return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);

应该是这个——

return secondaryFuel = (hours[1] * 60 + minutes[1]) * fuelSecondaryFactor;

或这个 -

return secondaryFuel = ((hours[1] * 60 + minutes[1]) * fuelSecondaryFactor);
于 2013-10-09T18:58:05.073 回答
2

更改最后一行:

return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);

return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor;
//                                                                         ^

编辑:-

代码也有额外的}括号。删除它们。确切地说},最后多了一个括号

于 2013-10-09T18:58:51.507 回答
1

)你这里有多余的

return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
                                 ^ 
于 2013-10-09T19:02:00.927 回答