🚀 Escape Room Javascript Hacks

  1. Simulate drawing two random cards, where each card has a value between 1 and 10. Show the two card values and their total.
%%js 
// 🎴 Simulate drawing two random cards between 1 and 10

// TODO: Generate a random value between 1 and 10 for the first card
let card1 = Math.floor(Math.random() * 10) + 1; // random integer 1-10
let card2 = Math.floor(Math.random() * 10) + 1; // random integer 1-10

let total = card1 + card2;

console.log("Card 1:", card1);
console.log("Card 2:", card2);
console.log("Total:", total);

<IPython.core.display.Javascript object>

Console Output

  1. Create a decision maker where “Definitely” appears 30% of the time
%%js
// 🎲 Decision Maker
// Goal: "Definitely" should appear about 30% of the time when you run this code

// TODO: Generate a random number between 1 and 100
let rand = Math.floor(Math.random() * 100) + 1;

if (rand <= 30) {
  console.log("Definitely");
} else {
  console.log("Not this time");
}

<IPython.core.display.Javascript object>

Console Output

  1. Simulate one coin flip and one dice roll.
  • The coin flip should be 0 = Heads or 1 = Tails.

  • The dice roll should be a number between 1 and 6.

  • Print out both results.

%%js
// 🪙 Simulate one coin flip and one dice roll

// TODO: Generate a random value 0 or 1 for the coin
let coin = Math.floor(Math.random() * 2);

console.log("Coin flip:", coin === 0 ? "Heads" : "Tails");

let dice = Math.floor(Math.random() * 6) + 1;

console.log("Dice roll:", dice);

<IPython.core.display.Javascript object>

Console Output

  1. Write code that randomly picks one fortune from a list of 5 possible fortunes and prints it out.
%%js 
// 🔮 Random Fortune Generator

// TODO: Make a list (array) of fortunes
let fortunes = [
  "You will have a great day!",
  "A surprise is waiting for you.",
  "Good luck is coming your way.",
  "Be cautious with new opportunities.",
  "Happiness is around the corner."
];

let index = Math.floor(Math.random() * fortunes.length);
console.log("Your fortune:", fortunes[index]);
<IPython.core.display.Javascript object>

Console Output