Site Title

POPCORN HACK 1 A short program about variables

%%html

<div id="outputPH1">Results will appear here:<br></div>

<script>

(() => {

const output = document.getElementById("outputPH1");

let name = "Adya";
let age = 14;

console.log("Hi, my name is " + name);
output.innerText += "Hi, my name is " + name + "\n";
console.log("I am " + age + " years old.");
output.innerText += "I am " + age + " years old.\n";

let nextYearAge = age + 1;

// TODO: Print out the result
// Example: "Next year I will be 11 years old."
console.log("Next year I will be " + nextYearAge + " years old.");
output.innerText += "Next year I will be " + nextYearAge + " years old.\n";

})();

</script>

Results will appear here:

POPCORN HACK 2 A short program about classes

%%html

<div>
  <h3>Program Output</h3>
  <div id="outputPH2">Results will appear here:<br></div>
</div>

<script>

(() => {

const output = document.getElementById("outputPH2");


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 + "!");
    output.innerText += this.name + " the " + this.kind + " says " + this.sound + "!\n";
  }

  // Bonus method: describe the animal
  describe() {
    console.log(this.name + " is a " + this.kind + " and is very friendly and smart!");
    output.innerText += this.name + " is a " + this.kind + " and is very friendly and smart!\n";
  }
}

// Step 2: Create a list to hold all the animals in the zoo
let zoo = [];

// Step 3: Add some animals to the zoo
zoo.push(new Animal("Bob", "Woof", "Dog"));
zoo.push(new Animal("Timmy", "Meow", "Cat"));
zoo.push(new Animal("Robert", "Squawk", "Parrot"));
zoo.push(new Animal("Salma", "Oink", "Pig"));


zoo.forEach(animal => {
  animal.speak();
  animal.describe();
});

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();

})();
</script>


Program Output

Results will appear here:

Homework

%%javascript

// 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: ");
}


<IPython.core.display.Javascript object>

Submission!

https://forms.gle/2aPP6CXFdQnNaE7GA