Varclasses
Teaching Variables and Classes
- POPCORN HACK 1 A short program about variables
- POPCORN HACK 2 A short program about classes
- Homework
- Sumbmission!
POPCORN HACK 1 A short program about variables
// Step 1: Make some variables
let name = "Sam";
let age = 10;
// Step 2: Print a message
console.log("Hi, my name is", name);
console.log("I am", age, "years old.");
// Step 3: Unfinished part
// TODO: Make a new variable called "nextYearAge"
// that is the age plus 1
// let nextYearAge = ??? // <-- finish this line!
// TODO: Print out the result
// Example: "Next year I will be 11 years old."
// console.log( ??? ) // <-- finish this line!
POPCORN HACK 2 A short program about classes
// Step 1: Define the Animal class
class Animal {
// Initialize each animal with a name, sound, and type
constructor(name, sound, kind) {
this.name = name;
this.sound = sound;
this.kind = kind;
}
// Make the animal speak
speak() {
console.log(`${this.name} the ${this.kind} says ${this.sound}!`);
}
// Bonus method: describe the animal
describe() {
console.log(`${this.name} is a ${this.kind} and is very friendly!`);
}
}
// Step 2: Create a list to hold all the animals in the zoo
let zoo = [];
// Step 3: Add animals to the zoo
// TODO: Create at least 3 animals and push them into the zoo array
// Example:
zoo.push(new Animal("Buddy", "Woof", "Dog"));
zoo.push(new Animal("Mittens", "Meow", "Cat"));
zoo.push(new Animal("Polly", "Squawk", "Parrot"));
// Step 4: Loop through all animals and make them speak
// TODO: Use a for loop (or forEach) to call speak() on each animal
zoo.forEach(animal => {
animal.speak();
// Step 5: Optional bonus: Call describe() too
animal.describe();
});
// Step 5 Bonus: Let the user add a new animal (works in browser)
// Uncomment if running in browser with prompt()
// let name = prompt("Enter the animal's name:");
// let sound = prompt("Enter the sound it makes:");
// let kind = prompt("Enter the kind of animal:");
// let newAnimal = new Animal(name, sound, kind);
// zoo.push(newAnimal);
// newAnimal.speak();
// newAnimal.describe();
Homework
// Step 1: Make a list of choices
const choices = ["rock", "paper", "scissors"];
// Step 2: Ask the user for their choice (browser version with prompt)
let userChoice = prompt("Choose rock, paper, or scissors:").toLowerCase();
// Step 3: Computer picks a random choice
const computerChoice = choices[Math.floor(Math.random() * choices.length)];
console.log("Computer chose:", computerChoice);
// Step 4: Compare userChoice and computerChoice
if (userChoice === computerChoice) {
console.log("It's a tie!");
} else if (userChoice === "rock" && computerChoice === "scissors") {
console.log("You win!");
} else if (userChoice === "scissors" && computerChoice === "paper") {
console.log("You win!");
} else if (userChoice === "paper" && computerChoice === "rock") {
console.log("You win!");
} else {
console.log("You lose!");
}
// Bonus: Put the whole game in a loop
// Uncomment to play multiple rounds in browser
/*
while (true) {
let userChoice = prompt("Choose rock, paper, or scissors”)
Sumbmission!
https://forms.gle/2aPP6CXFdQnNaE7GA