-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
43 lines (36 loc) · 1.45 KB
/
Copy pathmain.js
File metadata and controls
43 lines (36 loc) · 1.45 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
'use strict';
// Ask the user's name and greet them
const userName = prompt("Greetings, and welcome to my webpage. What name do you perfer to be called?");
alert(`Welcome, ${userName}! Let's play the About Me guessing game.`);
// Function to validate yes/no answers
function validateYesNo(answer) {
return (answer.toLowerCase() === 'yes' || answer.toLowerCase() === 'no' ||
answer.toLowerCase() === 'y' || answer.toLowerCase() === 'n');
}
// Questions and Answers
const questions = [
"Do I enjoy coding?",
"Do I have a degree in Computer Science??",
"Have I worked as a software engineer for 3 years?",
"Is my goal to become a full-stack developer?",
"Is my favorite color blue?"
];
const answers = [true, true, true, false, true];
// Game logic
for (let i = 0; i < questions.length; i++) {
let userAnswer = prompt(questions[i]);
if (validateYesNo(userAnswer)) {
if ((userAnswer.toLowerCase() === 'yes' || userAnswer.toLowerCase() === 'y') === answers[i]) {
alert("Correct!");
console.log(`Question ${i + 1}: User answered correctly.`);
} else {
alert("Incorrect.");
console.log(`Question ${i + 1}: User answered incorrectly.`);
}
} else {
alert("Please enter a valid yes or no answer.");
i--; // Ask the same question again
}
}
// Final message to the user
alert(`Thanks for playing, ${userName}! I hope you got to know me better.`);