-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
133 lines (116 loc) · 3.38 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let word = "";
let wordLength = 5;
let noOfWords = 1;
let isSwears = false;
let hint = "Not available right now pookie";
let settingsClicked = false;
function displaySettings() {
settingsClicked = !settingsClicked;
var dropdownElement = document.querySelector(".dropdown");
if (settingsClicked) {
dropdownElement.classList.add("unhide");
} else {
dropdownElement.classList.remove("unhide");
}
}
function settingsChanged() {
wordLength = document.getElementById("wordLength").value;
isSwears = document.getElementById("swears").checked;
displaySettings();
windowsOnLoad();
}
function checkEnter(event) {
if (event.key === "Enter") {
compareWord();
}
}
function compareWord() {
if (document.getElementById("checkButton")) {
enteredWord = document.getElementById("text").value;
if (enteredWord.toUpperCase().trim() === word.toUpperCase().trim()) {
animation.innerHTML = '<img src="assets/tick.gif" alt="Tick Animation">';
document.getElementById("hint").textContent = "Hint : ";
document.getElementById("text").value = "";
windowsOnLoad();
} else {
animation.innerHTML =
'<img src="assets/cross.gif" alt="Cross Animation">';
}
console.log(word);
console.log(enteredWord);
}
setTimeout(() => {
animation.innerHTML = "";
document.getElementById("text").value = "";
}, 1800);
}
function generateUnjumbledWord(word) {
let jumbledWord = word;
for (var i = 0; i < jumbledWord.length; i++) {
let randomIndex = Math.floor(Math.random() * 5);
let temp = jumbledWord[i];
jumbledWord =
jumbledWord.substring(0, i) +
jumbledWord.substring(randomIndex, randomIndex + 1) +
jumbledWord.substring(i + 1);
jumbledWord =
jumbledWord.substring(0, randomIndex) +
temp +
jumbledWord.substring(randomIndex + 1);
}
return jumbledWord;
}
function removePunctuation(word) {
const punctuationRegex = /[!#$%&()*+-.:;<=>?@[\]^_`{|}~]/g;
const cleanedWord = word.replace(punctuationRegex, "");
return cleanedWord;
}
function windowsOnLoad() {
fetchWord();
fetchHint();
}
function fetchWord() {
fetch(
`https://unjumble-word-api.onrender.com/?wordLength=${wordLength}&noOfWords=${noOfWords}&isSwears=${isSwears}`
)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
word = data.words[0];
document.getElementById("word").textContent =
generateUnjumbledWord(word).toUpperCase();
})
.catch((error) => {
console.error("Error:", error);
});
}
function fetchHint() {
fetch(`https://unjumble-api.onrender.com/?word=${word}`)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
hint = data.hint;
hint = removePunctuation(hint);
document.getElementById("hint").textContent = "Hint : " + hint;
})
.catch((error) => {
console.error("Error:", error);
});
}
window.addEventListener("load", () => {
windowsOnLoad();
setTimeout(() => {
document.querySelector(".loader").classList.add("loader--hidden");
document.querySelector(".loader").addEventListener("transitionend", () => {
document.body.removeChild(document.querySelector(".loader"));
});
}, 2400);
});