Day 2: Operators

Day 2: Operators

Objective

In this challenge, you’ll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video!

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.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result!

Input Format

There are 3 lines of numeric input:

The first line has a double, mealCost (the cost of the meal before tax and tip).

The second line has an integer, tipPercent (the percentage of mealCost being added as tip).

The third line has an integer, taxPercent (the percentage of mealCost being added as tax).

Output Format

Print the total meal cost, where totalCost is the rounded integer result of the entire bill (mealCost with added tax and tip).

Sample Input 0

1
2
3
12.00
20
8

##Sample Output 0

1
15

Explanation

Given:
mealCost = 12, tipPercent = 20, taxPercent = 8

Calculations:

  • tip = 12 x = 2.4
  • tax = 12 x = 0.96
  • totalCost = mealCost + tip + tax = 12 + 2.4 + 0.96 = 15.36
  • round(totalCost) = 15

We round totalCost to the nearest dollar (integer) and then print our result, 15.




Solutions

Solution 1

1
2
3
4
5
6
7
8
9
function main() {
var cost = +input_stdin_array[0];
var tip = cost * input_stdin_array[1] / 100;
var tax = cost * input_stdin_array[2] / 100;
var total = cost + tip + tax;
var round = Math.round(total);

console.log('The total meal cost is ' + round + ' dollars.');
}

Solution 2

1
2
3
4
5
6
// Complete the solve function below.
function solve(meal_cost, tip_percent, tax_percent) {
let totalCost = meal_cost * (1 + (tip_percent / 100) + (tax_percent / 100));

console.log(Math.round(totalCost));
}

Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×