-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex3.html
More file actions
86 lines (79 loc) · 3.85 KB
/
Copy pathindex3.html
File metadata and controls
86 lines (79 loc) · 3.85 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
84
85
86
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Coding Adventure</title>
<style>
body { font-family: Arial, sans-serif; }
#gameContainer { max-width: 600px; margin: 0 auto; padding: 20px; }
#story { font-size: 1.2em; margin-bottom: 20px; }
#codeInput { width: 100%; padding: 10px; margin-bottom: 10px; }
#submitButton { padding: 10px 20px; }
#feedback { margin-top: 20px; }
</style>
</head>
<body>
<div id="gameContainer">
<div id="story"></div>
<textarea id="codeInput" placeholder="Enter your JavaScript code here..."></textarea>
<button id="submitButton">Submit</button>
<div id="feedback"></div>
</div>
<script>
let currentStep = 0;
let startTime = 0;
let points = 0;
const stories = [
{
text: "Welcome to the JavaScript Coding Adventure! Let's start with the basics. Your first task is to write a function that returns 'Hello, World!'.",
correctAnswer: "function greet() { return 'Hello, World!'; }"
},
{
text: "Great job! Now, let's create a function that takes a name as an argument and greets the person. For example, if the name is 'Alice', it should return 'Hello, Alice!'.",
correctAnswer: "function greet(name) { return `Hello, ${name}!`; }"
},
{
text: "Excellent! Now, let's work with arrays. Write a function that takes an array of numbers and returns the sum of all the numbers in the array.",
correctAnswer: "function sumArray(numbers) { return numbers.reduce((total, num) => total + num, 0); }"
},
{
text: "You're doing amazing! Now, let's create a function that takes an array of strings and returns a new array with all the strings in uppercase.",
correctAnswer: "function toUpperCaseArray(strings) { return strings.map(str => str.toUpperCase()); }"
},
{
text: "Almost there! Finally, write a function that takes an object and returns a new object with all the keys and values swapped.",
correctAnswer: "function swapKeysValues(obj) { return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key])); }"
}
];
function startGame() {
document.getElementById('story').innerText = stories[currentStep].text;
startTime = new Date().getTime();
}
function checkAnswer() {
const userAnswer = document.getElementById('codeInput').value;
const correctAnswer = stories[currentStep].correctAnswer;
const endTime = new Date().getTime();
const timeTaken = (endTime - startTime) / 1000; // in seconds
if (userAnswer.trim() === correctAnswer.trim()) {
points += 100 - Math.floor(timeTaken * 10); // Award points based on speed
currentStep++;
if (currentStep < stories.length) {
startGame();
} else {
document.getElementById('story').innerText = `Congratulations! You've completed the JavaScript Coding Adventure! Your final score is ${points} points.`;
document.getElementById('codeInput').disabled = true;
document.getElementById('submitButton').disabled = true;
}
} else {
document.getElementById('feedback').innerText = 'Incorrect! Please try again.';
}
}
document.getElementById('submitButton').addEventListener('click', checkAnswer);
startGame();
</script>
<script>
console.log("hello world")
</script>
</body>
</html>