[JavaScript] JavaScript Fundamentals – Part 1 Coding Challenge #4

Photo by Joan Gamell on Unsplash

[JavaScript] JavaScript Fundamentals – Part 1 Coding Challenge #4

Steven wants to build a very simple tip calculator for whenever he goes eating in a restaurant. In his country, it's usual to tip 15% if the bill value is between 50 and

  1. If the value is different, the tip is 20%. Your tasks:
  2. Calculate the tip, depending on the bill value. Create a variable called 'tip' for this. It's not allowed to use an if/else statement 😅 (If it's easier for you, you can start with an if/else statement, and then try to convert it to a ternary operator!)
  3. Print a string to the console containing the bill value, the tip, and the final value (bill + tip). Example: “The bill was 275, the tip was 41.25, and the total value 316.25” Test data: § Data 1: Test for bill values 275, 40 and 430
const bill = 430;
const tip = bill >= 50 && bill <= 300 ? bill * 0.15 : bill * 0.2;
const totalValue = bill + tip;
console.log(`The bill was ${bill}, the tip was ${tip}, and the total value was ${totalValue}`);

Bill was 275,

code challenge 4-1.PNG

Bill was 40,

code challenge 4-2.PNG

Bill was 430,

code challenge 4-3.PNG