-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.3_shipping.html
55 lines (45 loc) · 1.74 KB
/
9.3_shipping.html
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
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html>
<head>
<title>AMAZON SHIPPING CALCULATOR</title>
</head>
<body>
<p>Amazon shipping calculator</p>
<input class="js-cost-input" type="text" placeholder="Cost of order" onkeydown="
// clicks and keydowns are known as events.
// and onclick= click and onkeydown = key press are known as event listerner.
//onscroll=scrolling ,onmouseenter= hovering over,onmouseleave= stop hovering over
handleCostkeydown(event);// and to save something inside the function used event in parameter
">
<button onclick="calculateTotal()">calculate</button>
<p class="js-total-cost"></p>
<script>
String(25)
console.log('25' - 5);// automatic type TYPE COERCION
console.log('25' + 5); // both works as string in adding so we use Number
// Window object- window. is a build-in object that represent the broswer
// window.document
// window.console.log('window');
// window.alert()
function handleCostkeydown(event) {
if (event.key === 'Enter') {// event object is not exixt in the function so used parameter put in function
calculateTotal();
}
}
function calculateTotal() {
const inputElement = document.querySelector('.js-cost-input');
// TYPE COERCION - error
// let cost =inputElement.value;
// console.log(cost);
// let cost = parseFloat(inputElement.value);
//let cost = inputElement.value * 1; // Implicitly convert input to a number
//console.log(typeof cost); string
let cost = Number(inputElement.value);
if (cost < 40) {
cost = cost + 10;
}
document.querySelector('.js-total-cost').innerHTML = `Total cost : $${cost}`; //cost.toFixed(2) can be used for 2 decimal place
}
</script>
</body>
</html>