DEV Community

pranav589
pranav589

Posted on

Day-2: 30 days of code- Hackerrank

Alt Text

Hey folks. On Day-2 of 30-Days of Code by HackerRank, we'll solve the question related to operators using Javascript.

Lets dive into it.

Day-0

Task:-

'Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost. Round the result to the nearest integer.'

In the task, we are given the meal price, tip percent and tax percent and we have to find the total cost of the meal which would be the addition of this three things(meal+tip+tax).

Solution:-

function solve(meal_cost, tip_percent, tax_percent) {
//total cost equation basic maths
const total_cost=meal_cost+(tip_percent*meal_cost/100)+(tax_percent*meal_cost/100)

//rounding the value to the nearest integer using Math.round
const rounded_cost=Math.round(total_cost)
console.log(rounded_cost)
}
Enter fullscreen mode Exit fullscreen mode

Explanation:-

  1. In the solution, we wrote a function solve(), which receives three parameters viz., meal_cost, tip_percent, tax_percent. The function call for solve() is already made for us and we just have to complete the code inside the function.
  2. Now, we declared a variable called as total_cost and assigned a simple mathematical equation to it to give the total meal cost.
  3. Then we rounded off our total meal cost to the nearest integer using Math.round() method (which is a built-in method of javascript).

Thank you!! Stay tuned!!

Top comments (3)

Collapse
 
dakujem profile image
Andrej Rypo

Did you test your solution? You didn't. If you did, you would be surprised to find out you are missing a return statement.

Don't get me wrong, I'm not scalding you. I'm trying to give you a valuable advice.
Learn to write tests as you learn to write code. It will make you a much better programmer. Trust me.

Collapse
 
dakujem profile image
Andrej Rypo

My bad... "Find and print"... the task is flawed 🤷‍♂️
Anyway, much better to write console.log(solve(...)) and return a result than print within the working function and retun nothing.

Collapse
 
pranav589 profile image
pranav589

It passed all the test cases provided by HackerRank. And ya thanks for your advice. It'll help me in the future for sure.