0036【Edabit ★☆☆☆☆☆】【让我加油 】Let's Fuel Up!
control_flow
language_fundamentals
numbers
Instructions
A vehicle needs 10 times the amount of fuel than the distance it travels. However, it must always carry a minimum of 100 fuel before setting off.
Create a function which calculates the amount of fuel it needs, given the distance.
Examples
javascript
calculateFuel(15) // 150
calculateFuel(23.5) // 235
calculateFuel(3) // 100
Notes
- Distance will be a number greater than zero.
- Return 100 if the calculated fuel turns out to be less than 100.
Solutions
javascript
function calculateFuel(n) {
let ans = n * 10;
return ans < 100? 100:ans;
}
TestCases
javascript
let Test = (function(){
return {
assertEquals:function(actual,expected){
if(actual !== expected){
let errorMsg = `actual is ${actual},${expected} is expected`;
throw new Error(errorMsg);
}
}
}
})();
Test.assertEquals(calculateFuel(15), 150)
Test.assertEquals(calculateFuel(23), 230)
Test.assertEquals(calculateFuel(10), 100)
Test.assertEquals(calculateFuel(3), 100)
Test.assertEquals(calculateFuel(23.5), 235)
Test.assertEquals(calculateFuel(3.14), 100)
Test.assertEquals(calculateFuel(9.99999), 100)
Test.assertEquals(calculateFuel(822315322), 8223153220)
Test.assertEquals(calculateFuel(12345.6789), 123456.789)
Test.assertEquals(calculateFuel(31.41), 314.1)