以下代码编译时出现以下错误:
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);
}
}
}
答案 0 :(得分: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);
答案 1 :(得分:2)
更改最后一行:
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
到
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor;
// ^
修改: - 强>
该代码还有额外的 }
括号。删除它们。确切地说,最后还有一个额外的 }
括号
答案 2 :(得分:1)
你这里有额外的)
return secondaryFuel = ((hours[1]) * 60 + minutes[1]) * fuelSecondaryFactor);
^