forked from 19smabtahinoor/TravelX-VanillaJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
43 lines (36 loc) · 1.23 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const marsConst = 5000
const moonCost = 3000
function handleClick(planet,operator){
//get the input
const quantityInput = document.getElementById(planet + '-tickets').value
//convert to number
let quantity = parseInt(quantityInput)
operator == 'plus' ? quantity += 1 : ((quantity > 0) ? quantity -= 1 : null)
//update the value
document.getElementById(planet + '-tickets').value = quantity
calculateTicketsCost(planet, quantity)
}
let marsTotal = 0;
let moonTotal = 0
//calculate the cost for tickets
function calculateTicketsCost(planet, quantity){
if (planet == 'mars') {
marsTotal = quantity * marsConst
updateUI(planet,marsTotal)
}else{
moonTotal = quantity * moonCost
updateUI(planet, moonTotal)
}
updateTotalCost(marsTotal, moonTotal )
}
//update total cost ui
function updateUI(planet,total){
const journeyCostInput = document.getElementById(planet + '-total')
journeyCostInput.innerText = total
}
//updateTotal cost
function updateTotalCost(marsTotal, moonTotal){
const totalJourneyCost = document.getElementById('total')
const total = marsTotal + moonTotal
totalJourneyCost.innerText = total
}