// 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;
If the rocketSpeed
is more than one second, then I get the correct value.
For example: const requiredTime = 17 * 1
;
But if you specify a rocketSpeed
less than 1 second, then the value is incorrect:
For example: const requiredTime = 17 * 0.625; // 10.625
0.625 is the number of rockets fired per second. That is, 1 rocket will be launched in about ~ 1.2 seconds.
I've tried different options by type: const requiredTime = 17 (17 - 37.5%);
(37.5% because: 1000 - 625 = 375. 375 is 37.5% of 1000), but it doesn't work.
CodePudding user response:
Number of rockets launched per second is better thought of as a frequency than a "speed".
Frequency f is f = 1/T where time is T, so T = 1/f
Maybe rename rocketSpeed to rocketFrequency and make a new variable timeBetweenRockets with
const timeBetweenRockets = 1 / rocketFrequency
.
Then the last line of code will be more selfexplaining
const requiredTime = numberOfRockets * timeBetweenRockets
CodePudding user response:
You have the calculation the wrong way round. If you try values greater than 1 then you'll see it takes longer: 1 rocket per second (r/s) results in 17 seconds. 2 r/s results in 34 seconds and 0.5 r/s gives 10.625 seconds.
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)}`);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
If you instead divide numberOfRockets
by rocketSpeed
you'll get the right answers.
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)}`);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>