-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise-grtr-20-bok-hotl-arith-awit.html
113 lines (96 loc) · 3.27 KB
/
promise-grtr-20-bok-hotl-arith-awit.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<html>
<head>
<title>Promises</title>
</head>
<body>
<marquee>shashank g - 1nt21is413 - web lab report - 16/05/2023 - Promises</marquee>
<br>
<hr>
1) Write a function numTest that takes a number as an argument and returns a Promise that tests if the value is greater than the value 20.
<br>
<button onclick="Numtest()">results</button>
<hr>
2) Write a JavaScript program to book a hotel only after booking a flight.
[Hint: To achieve this, the promise returned from the bookHotel function is resolved only after resolving the promise from bookFlight function. If the promise gets rejected from bookflight then it won't execute the second function.]
<br>
<button onclick="flighthotel()">result</button>
<hr>
3) Implement a JavaScript promise to perform arithmetic operations. Display result for each operation synchronously using await () method. (Give delay in each promise object using settimeout() method).
<br>
<button onclick="performOperations()">result</button>
<hr>
<script>
function Numtest(){
function numTest(number) {
return new Promise(function(resolve, reject) {
if (number > 20) {
resolve(number + " is greater than 20");
} else {
reject(number + " is not greater than 20");
}
});
}
var input = prompt("Enter a number:");
var number = parseFloat(input);
numTest(number)
.then(function(result) {
alert(result);
})
.catch(function(error) {
alert(error);
});
}
function flighthotel() {
let ans = prompt("Have you booked the flight ticket? yes/no");
let flight= new Promise(function(resolve,reject){
if(ans=='Yes' || ans =='yes'){
resolve("You can book the hotel!");}
else{
reject('You cannot book the hotel,Please book the flight first'); }
});
flight.then(function successValue(result){
let ans1 = prompt("Have you booked the hotel? yes/no");
let hotel= new Promise(function(resolve1,reject1){
if(ans1=='Yes' || ans1 =='yes'){
resolve1("Hotel booked successfully!");}
else{
reject1('You can book the hotel now!'); }
});
hotel.then(function successValue(result)
{
alert(result);
},)
hotel.catch(function errorValue(result){
alert(result);
})
},)
flight.catch(function errorValue(result){
alert(result);
})
}
function performOperations() {
return new Promise(function(resolve) {
const a = parseInt(prompt("Enter the first number:"));
const b = parseInt(prompt("Enter the second number:"));
setTimeout(function() {
const sum = a + b;
alert("Sum: " + sum);
setTimeout(function() {
const difference = a - b;
alert("Difference: " + difference);
setTimeout(function() {
const product = a * b;
alert("Product: " + product);
setTimeout(function() {
const quotient = a / b;
alert("Quotient: " + quotient);
resolve();
}, 1000);
}, 1000);
}, 1000);
}, 1000);
});
}
</script>
</body>
</html>