1- // Your code goes in here
1+ // document.querySelector("#app").innerText = "Tip Calculator";
2+ const calculateButton = document . getElementById ( 'calculate-button' ) ;
3+ const CalculateTip = ( ) => {
4+ const amount = document . getElementById ( 'amount' ) ;
5+ const service = document . getElementById ( 'service' ) ;
6+ const sharingPeople = document . getElementById ( 'sharingPeople' ) ;
7+ const result = document . getElementById ( 'result' ) ;
8+
9+ let tipAmount = ( parseFloat ( amount . value ) + ( parseFloat ( amount . value ) / 100 ) * parseFloat ( service . value ) ) / parseFloat ( sharingPeople . value ) ;
10+ let amountFlag = false ;
11+ let sharingPeopleFlag = false ;
12+ if ( amount . value === '' || service . value === '' || sharingPeople . value === '' ) {
13+ alert ( 'Fill in all the fields!' )
14+ } else {
15+ if ( ! isNaN ( amount . value ) ) {
16+ amountFlag = true
17+ } else {
18+ amount . value = `invalid Number`
19+ amount . style . color = 'red' ;
20+ }
21+
22+ if ( ! isNaN ( sharingPeople . value ) ) {
23+ sharingPeopleFlag = true
24+ } else {
25+ sharingPeople . value = `invalid Number`
26+ sharingPeople . style . color = 'red' ;
27+ }
28+ if ( amountFlag === true && sharingPeopleFlag === true ) {
29+ if ( parseFloat ( sharingPeople . value ) !== 1 ) {
30+ result . textContent = `TIP AMOUNT $${ tipAmount } each`
31+ } else {
32+ result . textContent = `TIP AMOUNT $${ tipAmount } `
33+ }
34+
35+ }
36+ }
37+ console . log ( amount . value ) ;
38+ }
39+
40+ calculateButton . addEventListener ( "click" , CalculateTip ) ;
241
3- document . querySelector ( "#app" ) . innerText = "Tip Calculator" ;
0 commit comments