// 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; Si la velocidad del rocketSpeed es más de un segundo, obtengo el valor correcto.
Por ejemplo: const requiredTime = 17 * 1 ;
Pero si especifica una velocidad de rocketSpeed inferior a 1 segundo, entonces el valor es incorrecto:
Por ejemplo: const requiredTime = 17 * 0.625; // 10.625
0,625 es el número de cohetes disparados por segundo. Es decir, se lanzará 1 cohete en aproximadamente ~ 1,2 segundos.
He probado diferentes opciones por tipo: const requiredTime = 17 + (17 - 37.5%); (37,5% porque: 1000 - 625 = 375. 375 es el 37,5% de 1000), pero no funciona.
El número de cohetes lanzados por segundo se considera mejor como una frecuencia que como una "velocidad".
La frecuencia f es f = 1/T donde el tiempo es T , entonces T = 1/f
Tal vez cambie el nombre de RocketSpeed a RocketFrequency y haga una nueva variable timeBetweenRockets con
const timeBetweenRockets = 1 / rocketFrequency .
Entonces la última línea de código será más autoexplicativa.
const requiredTime = numberOfRockets * timeBetweenRockets
Tienes el cálculo al revés. Si prueba valores superiores a 1, verá que tarda más: 1 cohete por segundo (r/s) da como resultado 17 segundos. 2 r/s da como resultado 34 segundos y 0,5 r/s da 10,625 segundos.
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)}`); Si, en cambio, divide número de numberOfRockets por velocidad de rocketSpeed , obtendrá las respuestas correctas.
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)}`);