Advertisement
dimipan80

Exams - Keep The Change

Nov 14th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Don Vlado likes to eat at expensive restaurants. In such restaurants it is accepted that a customer should tip
  2. (leave extra change when paying his bill). However, don Vlado happens to be very stingy and wants to spare every penny
  3. he can when tipping at his favorite restaurant. Help him by calculating his exact tip!
  4.  Don Vlado's tip very much depends on his mood: When happy, don Vlado tips for 10% of the bill;
  5. 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
  6. 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,
  7. so Don Vlado leaves the tip 303 = 30 * 30 * 30 = 27000); In every other scenario, don Vlado is simply grumpy
  8. and tips for only 5% of the bill.
  9. The input data will be received as an array. It contains two arguments – the first one is don Vlado’s bill.
  10. The second one is don Vlado’s mood. The output consists of only one line – don Vlado’s tip, rounded to 2 places
  11. after the decimal point. */
  12.  
  13. "use strict";
  14.  
  15. function calculateVladoTip(arr) {
  16.         var bill = Number(args[0]);
  17.     var mood = args[1];
  18.  
  19.     switch (mood) {
  20.             case 'happy':
  21.                 console.log((bill / 10).toFixed(2));
  22.                     break;
  23.         case 'married':
  24.             console.log((bill / 2000).toFixed(2));
  25.             break;
  26.         case 'drunk':
  27.             var tip = (bill * 15) / 100;
  28.             var firstDigit = Number((tip.toString(10)).charAt(0));
  29.             console.log((Math.pow(tip, firstDigit)).toFixed(2));
  30.             break;
  31.         default:
  32.             console.log((bill / 20).toFixed(2));
  33.             break;
  34.     }
  35. }
  36.  
  37. console.log(calculateVladoTip(['120.44', 'happy']));
  38. console.log(calculateVladoTip(['1230.83', 'drunk']));
  39. console.log(calculateVladoTip(['716.00', 'bored']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement