// Number of rockets launched per second.
const rocketSpeed = 1;
// The amount of damage dealt by rocket.
const attackDamage = 60;
// The amount of hit points of the building.
const buildingHitPoints = 1000;
// The number of rockets required to destroy the building.
const numberOfRockets = Math.ceil(buildingHitPoints / attackDamage);
// The time it takes to destroy the building.
const requiredTime = numberOfRockets * rocketSpeed;
如果rocketSpeed 超過一秒,那么我得到正確的值。
例如:const requiredTime = 17 * 1;
但是如果你指定的rocketSpeed小于 1 秒,那么該值是不正確的:
例如: const requiredTime = 17 * 0.625; // 10.625
0.625 是每秒發射的火箭數。也就是說,1 枚火箭將在大約 1.2 秒內發射。
我按型別嘗試了不同的選項:const requiredTime = 17 (17 - 37.5%);
(37.5% 因為:1000 - 625 = 375。375 是 1000 的 37.5%),但它不起作用。
uj5u.com熱心網友回復:
每秒發射的火箭數量最好被認為是頻率而不是“速度”。
頻率f是f = 1/T,其中時間是T,所以T = 1/f
也許重命名rocketSpeed到rocketFrequency,并作出新的變數timeBetweenRockets用
const timeBetweenRockets = 1 / rocketFrequency。
那么最后一行代碼會更不言自明
const requiredTime = numberOfRockets * timeBetweenRockets
uj5u.com熱心網友回復:
您的計算方式錯誤。如果您嘗試大于 1 的值,那么您會看到它需要更長的時間:每秒 1 枚火箭 (r/s) 結果為 17 秒。2 r/s 結果為 34 秒,0.5 r/s 為 10.625 秒。
const calculate = rocketSpeed => {
// The amount of damage dealt by rocket.
const attackDamage = 60;
// The amount of hit points of the building.
const buildingHitPoints = 1000;
// The number of rockets required to destroy the building.
const numberOfRockets = Math.ceil(buildingHitPoints / attackDamage);
// The time it takes to destroy the building.
const requiredTime = numberOfRockets * rocketSpeed;
return requiredTime;
}
console.log(`Time for 1 rocket/s: ${calculate(1)}`);
console.log(`Time for 2 rocket/s: ${calculate(2)}`);
console.log(`Time for 0.625 rocket/s: ${calculate(0.625)}`);
如果改為劃分numberOfRockets的rocketSpeed,你會得到正確的答案。
const calculate2 = rocketSpeed => {
// The amount of damage dealt by rocket.
const attackDamage = 60;
// The amount of hit points of the building.
const buildingHitPoints = 1000;
// The number of rockets required to destroy the building.
const numberOfRockets = Math.ceil(buildingHitPoints / attackDamage);
// The time it takes to destroy the building.
const requiredTime = numberOfRockets / rocketSpeed;
return requiredTime;
}
console.log(`Time for 1 rocket/s: ${calculate2(1)}`);
console.log(`Time for 2 rocket/s: ${calculate2(2)}`);
console.log(`Time for 0.625 rocket/s: ${calculate2(0.625)}`);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374613.html
標籤:javascript 数学
上一篇:如何使用JavaScript使用一個作為參考來同步兩個不同大小框的滾動
下一篇:在給定距離的直線右側找到一個點
