🛠️

Helper Functions

Station 5: These are the scaffolding tools Grace provides. randomBetween() generates random numbers, coinFlip() returns true/false randomly, and choose() picks between two values.
// ============================================================
// HELPER FUNCTIONS (Grace's scaffolding tools)
// ============================================================

function randomBetween(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function coinFlip() {
    return Math.random() < 0.5;
}

function choose(condition, ifTrue, ifFalse) {
    return condition ? ifTrue : ifFalse;
}
🔒

Constants (Facts)

Station 3: These use const because they never change. HAP's name, model number, and creation year are permanent facts.
// ============================================================
// ROBOT FACTS (const - these never change)
// ============================================================
const robotName = "HyBit A. ProtoBot";
const modelNumber = "HAP-7000";
const creationYear = 2024;
🔓

Variables (State)

Station 3 + 4: These use let because they change. Notice the different data types: numbers (energyLevel), strings (statusMessage), and values from helper functions.
// ============================================================
// ROBOT STATUS (let - these change during operation)
// ============================================================
let energyLevel = 100;
let tasksDone = 0;
let statusMessage = "Ready to learn!";

// ============================================================
// DYNAMIC VALUES (using helper functions)
// ============================================================
let todaysChallenge = randomBetween(1, 10);
let currentMood = choose(energyLevel > 50, "Energized!", "Need rest...");

Complete Task Function

Station 5: See operators in action! Arithmetic (-, +), comparison (<, >), and template literals (`$`).
// ============================================================
// COMPLETE TASK FUNCTION
// ============================================================
function completeTask() {
    // Check if we have enough energy
    if (energyLevel < 15) {
        statusMessage = "Too tired! Need rest first.";
        updateDisplay();
        logToConsole("Cannot complete task - energy too low!");
        return;
    }

    // Use operators to update values
    const energyCost = 15;
    energyLevel = energyLevel - energyCost;    // Arithmetic: subtract
    tasksDone = tasksDone + 1;                 // Arithmetic: add

    // Use helper functions
    todaysChallenge = randomBetween(1, 10);
    currentMood = choose(energyLevel > 50, "Energized!", "Getting tired...");

    // Update display with template literal
    statusMessage = `Completed task #${tasksDone}!`;
    updateDisplay();
    logToConsole(`Task completed! Energy: ${energyLevel}, Tasks: ${tasksDone}`);
}
😴

Rest Function

Station 5: More operators and the choose() helper. Notice how we cap energy at 100 using a comparison.
// ============================================================
// REST FUNCTION
// ============================================================
function takeRest() {
    const restAmount = 25;
    energyLevel = energyLevel + restAmount;   // Arithmetic: add

    // Comparison: don't exceed 100
    if (energyLevel > 100) {
        energyLevel = 100;
    }

    // Helper function with comparison
    currentMood = choose(energyLevel > 50, "Energized!", "Recovering...");
    statusMessage = "Feeling refreshed!";
    updateDisplay();
    logToConsole(`Rested! Energy restored to: ${energyLevel}`);
}

🤖 Try Asking AI About This Code

← Back to Station 6