3.9 Developing Algorithms
Categories: Programming Fundamentals TutorialLearn how to develop algorithms through the Peppa Pig Maze game
๐ท Peppaโs Algorithm Adventures - JavaScript Hacks
Welcome to Peppaโs JavaScript algorithm challenges! Complete these three hacks to master algorithm development with interactive JavaScript examples.
Hack 1: Daddy Pigโs Car Speed Checker ๐
Help Daddy Pig check if his car is going the right speed using Boolean expressions!
Your task: Complete the missing Boolean conditions to validate car speeds.
%%javascript
function checkSpeedLimit(currentSpeed, speedLimit) {
// โ
Boolean condition: safe if current speed is less than or equal to the limit
const isSafe = currentSpeed <= speedLimit;
if (isSafe) {
console.log(`โ
Speed ${currentSpeed} mph is safe! (Limit: ${speedLimit} mph)`);
} else {
console.log(`โ Speed ${currentSpeed} mph is too fast! (Limit: ${speedLimit} mph)`);
}
return isSafe;
}
function compareCarSpeeds(speed1, speed2) {
// โ
Boolean expression: car 1 is faster if speed1 > speed2
const fasterCar = speed1 > speed2 ? "Car 1" : "Car 2";
console.log(`๐๏ธ ${fasterCar} is faster! (${speed1} vs ${speed2} mph)`);
return fasterCar;
}
function daddyPigDriving() {
console.log("๐ Daddy Pig's Driving Algorithm");
const currentSpeed = 35;
const schoolZoneLimit = 25;
// โ
Boolean condition: should slow down if current speed is greater than the limit
const shouldSlowDown = currentSpeed > schoolZoneLimit;
if (shouldSlowDown) {
console.log("๐ท Daddy Pig says: 'Oops! I should slow down!'");
} else {
console.log("๐ท Daddy Pig says: 'Perfect speed for the school zone!'");
}
}
// Test the algorithms (like the lesson does)
console.log("=== Testing Speed Algorithms ===");
checkSpeedLimit(30, 35);
checkSpeedLimit(40, 35);
console.log("\n=== Comparing Car Speeds ===");
compareCarSpeeds(45, 38);
console.log("\n=== Daddy Pig's Drive ===");
daddyPigDriving();
<IPython.core.display.Javascript object>

Hack 2: Suzy Sheepโs Playground Game ๐ฎ
Help Suzy Sheep create a simple playground game using conditional statements!
Your task: Complete the if/else conditions for Suzyโs interactive game.
%%javascript
// Suzy Sheep's playground game algorithm (similar to lesson's decision examples!)
function playgroundEntryChecker() {
console.log("๐ช Suzy's Playground Entry Checker");
const playerAge = 8;
const minimumAge = 5;
const maximumAge = 12;
// โ
Boolean conditions
const oldEnough = playerAge >= minimumAge;
const youngEnough = playerAge <= maximumAge;
// โ
Logical AND condition
const canEnter = oldEnough && youngEnough;
if (canEnter) {
console.log(`โ
Welcome to the playground! Age ${playerAge} is perfect!`);
} else {
console.log(`โ Sorry, age ${playerAge} is not in the range ${minimumAge}-${maximumAge}`);
}
return canEnter;
}
function suzyGameChooser(weather, hasKite) {
console.log("๐ Suzy's Game Choice Algorithm");
console.log(`Weather: ${weather}, Has kite: ${hasKite}`);
let activity;
// โ
Fill in the activities
if (weather === "sunny") {
if (hasKite) {
activity = "Flying kites! ๐ช";
} else {
activity = "Playing tag with friends! ๐โโ๏ธ"; // sunny but no kite
}
} else if (weather === "rainy") {
activity = "Jumping in muddy puddles! ๐ท๐ฆ"; // rainy day fun
} else {
activity = "Drawing and crafts under the gazebo! ๐จ"; // default / cloudy
}
console.log(`๐ฏ Suzy chooses: ${activity}`);
return activity;
}
function interactivePlaygroundGame() {
console.log("\n๐ฎ Interactive Playground Test");
// Simulate user choices
const choices = ["sunny", "rainy", "cloudy"];
const randomWeather = choices[Math.floor(Math.random() * choices.length)];
const hasKite = Math.random() > 0.5; // Random true/false
console.log(`๐ฒ Random scenario: Weather is ${randomWeather}, kite available: ${hasKite}`);
const gameChoice = suzyGameChooser(randomWeather, hasKite);
const canPlay = playgroundEntryChecker();
if (canPlay) {
console.log(`๐ Suzy is playing: ${gameChoice}`);
} else {
console.log("๐ข Suzy can't play today");
}
}
// โ
Run the playground algorithms
playgroundEntryChecker();
suzyGameChooser("sunny", true);
suzyGameChooser("rainy", false);
interactivePlaygroundGame();
<IPython.core.display.Javascript object>

Hack 3: Pedro Ponyโs Simple Navigation System ๐งญ
Help Pedro Pony navigate around his classroom using boundary checking!
Your task: Complete the Boolean conditions to control Pedroโs movement safely.
%%javascript
function pedroNavigationSystem() {
console.log("๐งญ Pedro's Classroom Navigation Algorithm");
// Pedro's current position in the classroom
let pedroX = 1;
let pedroY = 2;
// Classroom boundaries (like the lesson's maze boundaries)
const minX = 0, maxX = 4;
const minY = 0, maxY = 3;
console.log(`๐ด Pedro is at desk (${pedroX}, ${pedroY})`);
console.log(`๐ Classroom size: ${maxX + 1} x ${maxY + 1} desks`);
// Test different movements
console.log("\n--- Testing Pedro's Movement ---");
// Try moving to the whiteboard (right)
const newX = pedroX + 2;
// โ
Pedro can move right as long as he stays within the max X boundary
const canReachWhiteboard = newX <= maxX;
console.log(`Move to whiteboard (${newX}, ${pedroY}): ${canReachWhiteboard ? 'โ
Can reach' : 'โ Too far'}`);
// Try moving to the library corner (up)
const newY = pedroY + 2;
// โ
Pedro can move up as long as he stays within the max Y boundary
const canReachLibrary = newY <= maxY;
console.log(`Move to library (${pedroX}, ${newY}): ${canReachLibrary ? 'โ
Can reach' : 'โ Out of bounds'}`);
// Try moving to the door (left)
const exitX = pedroX - 2;
// โ
Pedro can move left as long as he stays above or equal to min X
const canReachDoor = exitX >= minX;
console.log(`Move to door (${exitX}, ${pedroY}): ${canReachDoor ? 'โ
Can reach' : 'โ Would hit wall'}`);
}
function interactiveClassroomNav() {
console.log("\n๐ฏ Interactive Classroom Navigation");
const startX = 2, startY = 1;
// Simulate different destinations
const destinations = [
{name: "teacher's desk", x: 4, y: 0},
{name: "reading corner", x: 0, y: 3},
{name: "supply closet", x: 3, y: 2}
];
const randomDest = destinations[Math.floor(Math.random() * destinations.length)];
console.log(`๐ฒ Pedro wants to go to: ${randomDest.name} at (${randomDest.x}, ${randomDest.y})`);
// โ
Boolean conditions for classroom boundaries
const validX = (0 <= randomDest.x) && (randomDest.x <= 4);
const validY = (0 <= randomDest.y) && (randomDest.y <= 3);
const canNavigate = validX && validY;
if (canNavigate) {
console.log(`โ
Pedro successfully navigated to the ${randomDest.name}!`);
} else {
console.log(`โ Can't reach ${randomDest.name} - it's outside the classroom!`);
}
}
// โ
Run Pedro's navigation algorithms
pedroNavigationSystem();
interactiveClassroomNav();
<IPython.core.display.Javascript object>

๐ What You Should Complete
After finishing the lesson, you should be able to:
- Hack 1: Fill in the Boolean comparison operators (
<=,>=,<,>) to make Daddy Pigโs speed checker work - Hack 2: Complete the
if/elsestatements and fill in missing activity choices for Suzyโs playground game - Hack 3: Fill in the boundary conditions for Pedroโs classroom navigation system