11// Your code goes in here
2+ // debugger;
3+ document . querySelector ( '#header' ) . innerText = 'Tip Calculator' ;
4+ calculate . addEventListener ( 'click' , calculateTip ) ;
5+ function calculateTip ( ) {
6+ const billAmount = document . getElementById ( 'bill-amount' ) ;
7+ const serviceRating = document . getElementById ( 'service-rating' ) ;
8+ const peopleCount = document . getElementById ( 'people-count' ) ;
9+ const calculate = document . getElementById ( 'calculate' ) ;
10+ const outPut = document . getElementById ( 'tip-amount' ) ;
11+ const each = document . getElementById ( 'each' ) ;
212
3- document . querySelector ( "#app" ) . innerText = "Tip Calculator" ;
13+ if (
14+ ! billAmount . value ||
15+ ! serviceRating . value ||
16+ ! peopleCount . value ||
17+ serviceRating . value === '--Choose an option--' ||
18+ billAmount . value === '0' ||
19+ peopleCount . value === '0'
20+ ) {
21+ alert ( 'All fields must be filled' ) ;
22+ input ;
23+ } else {
24+ billAmount . value === billAmount . innerText ;
25+ serviceRating . value === serviceRating . innerText ;
26+ peopleCount . value === peopleCount . innerText ;
27+ if ( peopleCount . value > 1 ) {
28+ each . innerText = 'each' ;
29+ } else {
30+ each . innerText = ' ' ;
31+ }
32+ let formatter = new Intl . NumberFormat ( 'en-US' , {
33+ style : 'currency' ,
34+ currency : 'USD' ,
35+ minimumFractionDigits : 2 ,
36+ } ) ;
37+
38+ switch ( serviceRating . value ) {
39+ case '30% - Outstanding' :
40+ outPut . innerText = formatter . format ( ( billAmount . value * ( 30 / 100 ) ) / peopleCount . value ) ;
41+ console . log ( outPut . innerText ) ;
42+ break ;
43+ case '20% - Good' :
44+ outPut . innerText = formatter . format ( ( billAmount . value * ( 20 / 100 ) ) / peopleCount . value ) ;
45+ break ;
46+ case '15% - It was OK' :
47+ outPut . innerText = formatter . format ( ( billAmount . value * ( 15 / 100 ) ) / peopleCount . value ) ;
48+ break ;
49+ case '10% - Bad' :
50+ outPut . innerText = formatter . format ( ( billAmount . value * ( 10 / 100 ) ) / peopleCount . value ) ;
51+ break ;
52+ case '5% - Terrible' :
53+ outPut . innerText = formatter . format ( ( billAmount . value * ( 5 / 100 ) ) / peopleCount . value ) ;
54+ break ;
55+
56+ default :
57+ break ;
58+ }
59+ }
60+
61+ // return (output.innerText = (billAmount.value * 10) / 100 + serviceRating.value);
62+ }
63+
64+ // console.log(billAmount.value);
65+ // calculateTip();
0 commit comments