Repository navigation
Expand file tree
/
Copy pathquiz-game.js
More file actions
54 lines (50 loc) · 1.5 KB
/
Copy pathquiz-game.js
File metadata and controls
54 lines (50 loc) · 1.5 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
const questions = [
{
category: "JavaScript",
question: "Which keyword declares a constant variable?",
choices: ["const", "let", "var"],
answer: "const"
},
{
category: "JavaScript",
question: "Which method converts a JavaScript object into a JSON string?",
choices: ["JSON.parse()", "JSON.stringify()", "JSON.convert()"],
answer: "JSON.stringify()"
},
{
category: "HTML",
question: "Which HTML tag is used to create a hyperlink?",
choices: ["<a>", "<link>", "<href>"],
answer: "<a>"
},
{
category: "CSS",
question: "Which CSS property changes the text color?",
choices: ["color", "font-size", "background-color"],
answer: "color"
},
{
category: "Programming",
question: "Which data structure follows the LIFO principle?",
choices: ["Stack", "Queue", "Tree"],
answer: "Stack"
}
];
function getRandomQuestion(array){
const random = Math.floor(Math.random() * array.length)
return array[random];
}
function getRandomComputerChoice(array){
const random = Math.floor(Math.random() * array.length)
return array[random]
}
function getResults(quesObj,computerChoice){
if(quesObj.answer === computerChoice){
return "The computer's choice is correct!";
}else {
return `The computer's choice is wrong. The correct answer is: ${quesObj.answer}`
}
}
const randomQues = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(randomQues.choices);
console.log(getResults(randomQues,computerChoice));