Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Don Vlado likes to eat at expensive restaurants. In such restaurants it is accepted that a customer should tip
- (leave extra change when paying his bill). However, don Vlado happens to be very stingy and wants to spare every penny
- he can when tipping at his favorite restaurant. Help him by calculating his exact tip!
- Don Vlado's tip very much depends on his mood: When happy, don Vlado tips for 10% of the bill;
- When married, don Vlado tips for 0.05% of the bill; When drunk, don Vlado tips for (15% of the bill)n, where n is
- the first digit of the tip. (e.g. if the bill is 200, 30 is 15% of the bill. 3 is the first digit of 30,
- so Don Vlado leaves the tip 303 = 30 * 30 * 30 = 27000); In every other scenario, don Vlado is simply grumpy
- and tips for only 5% of the bill.
- The input data will be received as an array. It contains two arguments – the first one is don Vlado’s bill.
- The second one is don Vlado’s mood. The output consists of only one line – don Vlado’s tip, rounded to 2 places
- after the decimal point. */
- "use strict";
- function calculateVladoTip(arr) {
- var bill = Number(args[0]);
- var mood = args[1];
- switch (mood) {
- case 'happy':
- console.log((bill / 10).toFixed(2));
- break;
- case 'married':
- console.log((bill / 2000).toFixed(2));
- break;
- case 'drunk':
- var tip = (bill * 15) / 100;
- var firstDigit = Number((tip.toString(10)).charAt(0));
- console.log((Math.pow(tip, firstDigit)).toFixed(2));
- break;
- default:
- console.log((bill / 20).toFixed(2));
- break;
- }
- }
- console.log(calculateVladoTip(['120.44', 'happy']));
- console.log(calculateVladoTip(['1230.83', 'drunk']));
- console.log(calculateVladoTip(['716.00', 'bored']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement