Reputation: 340
function calculatePrice(base_cost, base_dist, additional_cost, additional_dist, total_dist) {
if (total_dist > base_dist) {
} else {
return base_cost;
}
}
function unitTest() {
var cost;
cost = calculatePrice(10,20,5,5,15);
if (cost != 10) console.log ("FAILED 1");
cost = calculatePrice(10,10,5,5,15);
if (cost != 15) console.log ("FAILED 2");
cost = calculatePrice(20,10,5,1,15);
if (cost != 45) console.log ("FAILED 3");
cost = calculatePrice(20,10,5,1,1);
if (cost != 20) console.log ("FAILED 4");
cost = calculatePrice(10,10,5,1,14);
if (cost != 30) console.log ("FAILED 5");
cost = calculatePrice(10,10,5,2,14);
if (cost != 20) console.log ("FAILED 6");
console.log('Test complete');
}
I want to calculate the function value of unitTest for passing.I pass only 1 and need to write some pass code in the calculatePrice function. Thanks a lot
Upvotes: 0
Views: 108
Reputation:
You need to use Math.ceil
Calculation will be like that
var dist_diff = total_dist - base_dist;
var time = Math.ceil(dist_diff / additional_dist);
return (time * additional_cost) + base_cost;
So, you will got the Test complete result with no errors.
Upvotes: 2