-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson-demo.js
147 lines (143 loc) Β· 6.47 KB
/
lesson-demo.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const lesson = {
title: "Learn Coding!",
overview: "This app will help you learn to code websites!",
prerequisites: `Here are some prerequisites:
* [Basic HTML](https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Getting_started)
* [Basic CSS](https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/CSS_basics)
* [Basic JS like creating variables](https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics)`,
steps: [
{
intro: "We can use JavaScript to store values like text and numbers in variables and include them in the page HTML elements.",
code: `let coder = 'your name';
document.querySelector("#greeting").textContent=coder;`,
guess: "What do you think this code will do? π€",
run: "Try it! Copy the code into the script..",
explore: `The variable appeared in the page, but how? Look at the code again.. π
* It created a variable named \`coder\`
* Gave the variable the value \`your name\`
* Grabbed a page element with \`greeting\` as its ID
* Set the element content to the variable value`,
change: `π§ OK, now change the value of the variable to your own name instead of \`your name\``,
recap: "π In this step you wrote a text variable into the page β we can also store more complex types of data and build the page elements based on them...",
ran: () => {
try {
// Check the var has been declared and is a non-empty string
return typeof coder == "string" && coder.length > 0;
} catch (error) {
return false;
}
},
done: () => {
try {
// Check the var has been changed
return coder != "your name";
} catch (error) {
return false;
}
},
},
{
intro: "We can create arrays to store collections of values and also include them in page elements.",
code: `let languages = ["HTML", "CSS", "JavaScript"];
languages.forEach((lang) => {
let button = document.createElement("button");
button.textContent=lang;
document.querySelector("#buttons")
.appendChild(button);
});`,
guess: "What do you think this code will do? π€",
run: "Try it! Copy the code into the script..",
explore: `A button for each language we listed appeared in the page, but how? Take another look at the code.. π
* It created an array with three coding language names
* Looped through the array, creating a button for each
* Set the button text to the current array element and added it to the page`,
change: `π§ Add another element to the array, this time for \`Python\``,
recap: "π This time we included a series of values in the page as buttons. If we wanted more complex data we would probably connect the website to a database!",
ran: () => {
try {
// Check the var has been declared and is non-empty
return (
typeof languages == "object" &&
languages.length > 0 &&
document.querySelectorAll("#buttons button").length > 0
);
} catch (error) {
return false;
}
},
done: () => {
try {
// Check the var has been extended
return languages.includes("Python");
} catch (error) {
return false;
}
},
},
{
intro: "Once our data values are built into the page, we can use them interactively to update the content dynamically.",
code: `let buttons = document.querySelectorAll("#buttons button");
buttons.forEach((btn) => {
btn.onclick = () => {
document.querySelector("#greeting").textContent =
coder + " β₯οΈ " + btn.textContent;
};
});`,
guess: "What do you think this code will do? π€",
run: "Try it! Copy the code into the script..",
explore: `When we click, a button the page updates with the value we selected.. Take a look again π
* It grabbed all of the buttons from the section
* Added a function to respond to clicks on each one
* Included the value of the clicked button alongside the name variable`,
change:
'π§ Maybe we love more than one language! Change the code in the `onclick` function to add the selected language to the existing text instead of replacing it, you can use the plus equals operator `+=` on the `textContent` property.',
recap: "π Make sure you hit the buttons a few times! We can build data values all over our website functionality..",
ran: () => {
try {
// Check the code
return document.querySelector("#buttons button").onclick != null;
} catch (error) {
return false;
}
},
done: () => {
try {
// Check the code has been changed
return (
document
.querySelector("#buttons button")
.onclick.toString()
.indexOf("+=") >= 0
);
} catch (error) {
return false;
}
},
},
],
project: {
name: "Cool project",
intro: "Look at the code and comments to learn what this project does β you have your own remix of it you can mess around with!",
code: "project/project.js",
extend: `Edit the script to display the user's guesses in the page:
* Add code inside the \`onclick\` function
* Append another new child element on each click
* Add the text value of the user's guess in a "p" element
β οΈ _If you get stuck look back at your \`script.js\` code..._`,
done: () => {
return document
.querySelector("#game button")
.onclick.toString().indexOf("appendChild") > -1
},
recap: "π‘ You'll see lots of fancy frameworks for building websites with JavaScript, but what we learned in this project works in the browser without installing anything!"
},
synthesis: {
reflect: "π What did you find hardest about this lesson? What was most fun about it? What else would you use it for?",
connect: `π£ [Share on the community forum](https://support.glitch.com)`,
link: `Keep developing your project to do whatever you like! Here are some links to try next:
* [MDN: Manipulating Documents](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Manipulating_documents)
* [MDN: Introduction to the DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction)
* [MDN: Introduction to events](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events)`
},
};
export default lesson;