-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (69 loc) · 2.65 KB
/
script.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
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
// CHECKPOINT 1
// Refactored WeatherChecker promise
function WeatherChecker(forecast, partyDate, userName) {
return new Promise((resolve, reject) => {
const friends = forecast === 'forecast is sunny' ? 20 : 10;
if (forecast === 'forecast is sunny') {
resolve(`${partyDate} - ${userName}, Sunny weather, enjoy picnic party, invite ${friends} guests`);
} else {
reject(`${partyDate} - ${userName}, Rainy weather, enjoy house party, invite ${friends} guests`);
}
});
}
// Event listener for the user form
document.querySelector('.user-form').addEventListener('submit', async function (e) {
e.preventDefault(); // Prevent the form from submitting the traditional way
const userName = document.getElementById('user-name').value;
const partyDate = document.getElementById('party-date').value;
const resultList = document.getElementById('result-list');
try {
const forecast = await randomForecast();
WeatherChecker(forecast, partyDate, userName)
.then(result => {
console.log(result);
const listItem = document.createElement('li');
listItem.textContent = result;
resultList.appendChild(listItem);
})
.catch(error => {
console.log(error);
const listItem = document.createElement('li');
listItem.textContent = error;
resultList.appendChild(listItem);
});
// Reset the form inputs to their default values
e.target.reset();
} catch (error) {
console.error("Error fetching the forecast: ", error);
}
});
// The randomForecast function
function randomForecast() {
return new Promise((resolve) => {
setTimeout(() => {
const isSunny = Math.random() < 0.5;
resolve(isSunny ? 'forecast is sunny' : 'forecast is rainy');
}, 1000); // 1 second delay to simulate an API call
});
}
// CHECKPOINT 2
function getData() {
fetch('https://onlineprojectsgit.github.io/API/WDEndpoint.json')
.then(response => {
// Check if the response is OK
if (response.ok) {
return response.json(); // Parse and return the JSON response
} else {
throw new Error('Request failed'); // If response is not OK, throw an error
}
})
.then(data => {
console.log(data); // Log the data to the console
return data; // Return the data for further processing
})
.catch(error => {
console.error(error.message); // Log any errors that occur during the fetch
});
}
// Call the function to execute the fetch
getData();