Skip to content
Advertisement

(HackerRank Day 2: Operators) Problem with Constructor

In this HackerRank challenge, I need to find the total meal cost by adding tip_percent which is 20% of the meal_cost and tax_percent which is 8% of the meal_cost and the meal_cost being $12. So the output must be a round number of 15 but my output comes out as $14.

It does seem to work properly with custom values like $12.50 for meal_cost which later totaled comes out as a rounded value of $16. What am I doing wrong here?

JavaScript

Advertisement

Answer

You are using integers. Integers are rounded, so you loose precision over the next calculation. Try using doubles and cast to int at the end.

JavaScript

And don’t reuse parameters inside your function. It is bad practice.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement