-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.js
71 lines (67 loc) · 2 KB
/
page.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
let timer = document.getElementById("timer");
let quoteDisplay = document.getElementById("quoteDisplay");
let quoteInput = document.getElementById("quoteInput");
let result = document.getElementById("result");
let submitBtn = document.getElementById("submitBtn");
let resetBtn = document.getElementById("resetBtn");
let spinner = document.getElementById('spinner');
let randomText = ""
let inputText = ""
let startSeconds = 0;
//clear timer
clearTimer = () => {
startSeconds = 0;
clearInterval(intervalId);
}
//start the timer
setTimeAndShow = () => {
intervalId = setInterval(startTimer, 1000)
}
startTimer = () => {
startSeconds = startSeconds + 1
timer.textContent = startSeconds + " Seconds"
}
//generating a random text using fetch
generateRandomText = () => {
let url = "https://apis.ccbp.in/random-quote";
let options = {
method: "GET",
};
spinner.classList.remove("d-none");
quoteDisplay.classList.add("d-none");
fetch(url, options)
.then(function(response) {
return response.json()
})
.then(function(jsonData) {
randomText = jsonData.content
console.log(jsonData)
spinner.classList.add("d-none");
quoteDisplay.classList.remove("d-none");
quoteDisplay.textContent = jsonData.content;
setTimeAndShow()
})
}
generateRandomText()
//on click reset button
resetRandomText = () => {
generateRandomText()
clearTimer()
quoteInput.value = ""
timer.textContent = "Wait..."
}
//on click submit button
checkContent = () => {
inputText = quoteInput.value
console.log(inputText)
console.log(randomText)
if (inputText === randomText) {
result.textContent = `You typed in ${startSeconds} Seconds`;
clearTimer()
timer.textContent = "Great !"
} else {
result.textContent = "You Typed Incorrect Sentence";
}
}
submitBtn.addEventListener('click', checkContent)
resetBtn.addEventListener('click', resetRandomText)