An activity to introduce JavaScript in 45-60m
NOTE: This lesson is designed for students who have an introductory knowledge of HTML and CSS, but no experience is required
Start by opening a web browser and navigating to the starter code: bit.ly/js-starter. Students should follow along and do the same thing in their browsers.
Wherever the word “blank” appears in the HTML section, update it to personalize your website! For example, where it says “I’m blank,” replace the “blank” with your name.
In the CSS section, change the background color, text color, and font!
Direct the students to click the “Fork” button in CodePen. They have to make an account in order to save. Students should make note of their pen’s URL so they can recover their code and share it!
Explain that JavaScript is the programming language of the web, that allows webpages to be interactive. Developers use JavaScript to trigger events, create animations, make games, and much more!
The next step for the CodePen site is to add functionality to the “Quiz Me” button. It should pop up a quiz that allows the user to answer some questions and receive a final score. This is possible with JavaScript!
Developers use functions in JavaScript to trigger certain actions. In this example, clicking the “Quiz Me” button will call a JavaScript function. That means it triggers some actions! alert is the command that lets developers show messages to the user on a webpage.
startQuiz function
function keyword, the function name, parentheses, and curly bracketsalert statement that welcomes the user
onclick attribute on the HTML buttonfunction startQuiz() {
alert("Welcome to my quiz 🙂");
}
prompt is a command that allows us to ask questions to the user. When the user answers, the computer stores that text in a variable. Developers can then reference that information in the future!
alert, above the }, create a couple new linesname variable. How can we ask the user for their name?
prompt to ask for the user’s namealert to say “Good luck” to the user (using their name!)var name = prompt("What is your name?");
alert("Good luck " + name);
alert, above the }, create a couple new linespoints variable
0)numberOfQuestions variable
2var points = 0;
var numberOfQuestions = 2;
if statements allow developers to change the program flow based on certain information. It works just like in English; for example, “if it is raining, I will wear a raincoat.” In code, the program will do different things based on what the user says! For the quiz, the points should go up every time the user answers correctly. So, “if the answer from the user is equal to the correct answer, increase the points!”
numberOfQuestions variable declaration, above the }, create a couple new linesprompt (e.g., “What is my name?”)answer1prompt, create an if statement
if, parentheses, ===, and curly brackets to set up the if++ to increment the value of the points variablevar answer1 = prompt("What is my name?");
if (answer1 === "Joseph") {
points++;
}
NOTE: The students should check the answer against their own name
answer2 instead of answer1 for the variablevar answer2 = prompt("Where do I live?");
if (answer2 === "Lakewood") {
points++;
}
NOTE: The students should check the answer against where they live
Finally, it is possible to use the points variable to determine an overall score. Do some math to calculate the percentage against the total questions!
NOTE: This section can be skipped and replaced with an
alertthat simply displays the number of points at the end
finalScore variablepoints variable by the numberOfQuestions variable
alert to show the final score to the uservar finalScore = (points / numberOfQuestions) * 100;
alert("Final score: " + finalScore + "%");
https://codepen.io/jmaxwell/pen/BaBVVrO
If there is time remaining, students can attempt the challenges. They are available at bit.ly/JS-Workshop (case-sensitive). They can choose which challenges to attempt, and the JavaScript challenges are toward the end of the list.