Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (70 loc) · 2.07 KB
/
Copy pathapp.js
File metadata and controls
83 lines (70 loc) · 2.07 KB
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
//Begin
alert("Are you Ready?!")
initialMessage();
//Range
let qtynumber = 10;
// Array to check if number already was sorted
let alreadySorted = [];
//Secret Number
let SN = chooseNumber();
// Active just to tests:console.log("Secret Number ", SN);
//Attempts
let ATTEMPT = 1;
//Function to show text in screen
function showText(TAG, TEXT){
let field = document.querySelector(TAG);
field.innerHTML = TEXT;
responsiveVoice.speak(TEXT, "UK English Female", {rate:1.2});
}
//Function Initial Message
function initialMessage(){
showText("h1","Secret Number Game");
showText("p","Select a number between 1 to 10");
showText("button", "GO");
}
//Function to try
function TRY(){
console.log("Clicked")
let HIT = document.querySelector("input").value;
let ATTWORD = ATTEMPT > 1? "Attempts":"Attempt";
if (HIT == SN){
alert(`You Got it ! The Secret Number is ${SN} \n (${ATTEMPT} ${ATTWORD}) `);
showText("h1", "Congratulations!");
showText("p",`The Secret Number is ${SN}`)
document.getElementById("restart").removeAttribute("disabled");
}else{
if(HIT<SN){
alert("Wrong...The Secret Number is Greater (Try Again)");
}else{
alert("Wrong...The Secret Number is Smaller (Try Again)");
}
eraseField();
} ATTEMPT ++;
}
//Function to choose a number
function chooseNumber() {
let numberChosen = parseInt(Math.random() * qtynumber + 1);
let qtyalreadySorted = alreadySorted.length;
if(qtyalreadySorted == qtynumber){
alreadySorted = [];
}
if(alreadySorted.includes(numberChosen)){
chooseNumber();
}else{
alreadySorted.push(chooseNumber);
return numberChosen;
}
}
//Function Erase field
function eraseField(){
HIT = document.querySelector("input");
HIT.value = "";
}
//Function to Restart the game
function restartGame(){
SN = chooseNumber();
eraseField();
ATTEMPT = 1;
initialMessage();
document.getElementById("restart").setAttribute("disabled", true);
}