Coding Concept - Lists
Understanding Lists in Python and JavaScript
// ===============================
// Popcorn Hack 1: Initialize Snake
// ===============================
// TODO: Make an array called snake with:
// - 1 head ("H")
// - 3 body segments ("B")
// - 1 tail ("T")
// Hint: arrays in JavaScript use [ ... ] with commas.
let snake = []; // <-- fill this in
console.log("Starting snake:", snake);
// ===============================
// Popcorn Hack 2: Growing the Snake
// ===============================
// TODO: The snake eats food 🥩
// - Add 2 tails ("T") to the END of the array
// - Use push() twice
// Example: snake.push("T")
// Your code here
console.log("Snake after eating:", snake);
// ===============================
// Popcorn Hack 3: Snake Loses a Tail
// ===============================
// TODO: The snake moves forward, tail disappears
// - Remove the LAST 2 elements
// - Use pop() twice
// Example: snake.pop()
// Your code here
console.log("Snake after losing tails:", snake);
// ===============================
// Popcorn Hack 4: Combining It All
// ===============================
// TODO:
// Step 1: Add 2 new heads ("H") to the START of the array
// → use unshift()
// Step 2: Remove 1 head from the START of the array
// → use shift()
// Step 3: Print the new snake and its first head
// Your code here
console.log("Snake after moving:", snake);
console.log("New first head is:", snake[0]);
// ===============================
// Check Your Understanding
// ===============================
// Start fresh with a new snake
snake = ["H", "B", "B", "T"];
// TODO: Run these in order
// 1. Add a head at the START (unshift)
// 2. Add a tail at the END (push)
// 3. Remove the 3rd element (index 2) with splice()
// Your code here
console.log("Final snake:", snake);
console.log("First head:", snake[0]);
console.log("Last element:", snake[snake.length - 1]);