codex-lv2-may-2025

How to Use Helper Functions in Your VSCode Projects

๐Ÿ“ฅ Step 1: Download the Helper Functions

Option A: Copy from Week 5 Directory

  1. Navigate to guides/week5-event-driven-apps/helpers.js
  2. Open the file and copy all the content
  3. Create a new file called helpers.js in your project folder

Quick Download: Download helpers.js directly to your project folder

Option B: Download from Repository

  1. Right-click on helpers.js in the Week 5 directory
  2. Select โ€œSave Asโ€ or โ€œDownloadโ€
  3. Save it to your project folder

๐Ÿ“ Step 2: Project Structure

Ensure your project has this file structure:

your-project/
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ script.js
โ”œโ”€โ”€ helpers.js
โ”œโ”€โ”€ styles.css
โ””โ”€โ”€ assets/
    โ””โ”€โ”€ images/

๐Ÿ”— Step 3: Include in HTML

Add the helpers.js file to your HTML before your main script.js file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Project</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <!-- Your HTML content here -->
    
    <!-- Include helper functions FIRST -->
    <script src="helpers.js"></script>
    
    <!-- Then include your main script -->
    <script src="script.js"></script>
</body>
</html>

Important: The order matters! helpers.js must come before script.js.


๐Ÿ”ง Step 4: VSCode Configuration (Optional)

For an even better development experience, you can also set up VSCode with custom settings:

Download VSCode Configuration

Setup Instructions

  1. Download the settings.json file
  2. Create a .vscode folder in your project root
  3. Place settings.json inside the .vscode folder
  4. Download jsconfig.json and place it in the root of your project folder (same level as index.html)
  5. Restart VSCode or reload the window

What This Provides

Note: This step is optional but provides a professional development environment setup with better JavaScript IntelliSense.


๐Ÿ“ Step 5: Use Helper Functions in Your Code

Now you can use any of the helper functions in your script.js file:

Example: Using Helper Functions

// Change text content
setText("title", "New Title");

// Change image source
setImageURL("myImage", "new-image.jpg");

// Change CSS properties
setProperty("myDiv", "background-color", "blue");

Example: Using onEvent Helper

// In your script.js file
onEvent("myButton", "click", function() {
    console.log("Button clicked!");
    setText("output", "Hello from helpers!");
});

Example: Using playSound Helper when a button is clicked

// Play a sound when something happens
onEvent("playSoundButton", "click", function() {
    playSound("assets/success.mp3");
    // Your other code here
});

๐ŸŽฏ Step 6: Example HTML with Matching IDs

Hereโ€™s a complete HTML example that includes all the IDs referenced in the script examples above:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Helper Functions Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <!-- Elements referenced in the script examples -->
        <h1 id="title">Original Title</h1>
        
        <div id="myDiv" class="content-box">
            <p>This div can have its background color changed</p>
        </div>
        
        <img id="myImage" src="default-image.jpg" alt="Example image">
        
        <button id="myButton">Click Me!</button>
        
        <button id="playSoundButton">Play Sound</button>
        
        <div id="output">Output will appear here</div>
        
        <!-- Test elements for the setup verification -->
        <button id="testButton">Test Button</button>
        <div id="testOutput">Test output will appear here</div>
    </div>
    
    <!-- Include helper functions FIRST -->
    <script src="helpers.js"></script>
    
    <!-- Then include your main script -->
    <script src="script.js"></script>
</body>
</html>

Note: All the IDs in this HTML (title, myDiv, myImage, myButton, playSoundButton, output, testButton, testOutput) match exactly with the IDs used in the JavaScript examples above.


๐Ÿ”ง Step 7: Available Helper Functions

Event Handling

DOM Manipulation

Audio

Note

These are the actual helper functions implemented in helpers.js. For native DOM methods like getElementById, querySelector, etc., use the standard browser APIs directly.


๐Ÿšจ Step 8: Common Issues and Solutions

โ€œFunction not definedโ€ Error

Helper Functions Not Working

Console Errors


๐Ÿ’ก Step 9: Best Practices

  1. Always load helpers first in your HTML
  2. Check the console for any error messages
  3. Test one function at a time to isolate issues
  4. Keep your helpers.js file unchanged - donโ€™t modify it
  5. Use the helper functions consistently across your project

๐Ÿ” Step 10: Testing Your Setup

Add this to your script.js to test if helpers are working:

// Test if helpers are loaded
console.log("Testing helper functions...");

// Test onEvent
onEvent("testButton", "click", function() {
    console.log("onEvent helper working!");
    setText("testOutput", "Helpers are working!");
});

console.log("Helper functions loaded successfully!");

If you see โ€œHelper functions loaded successfully!โ€ in the console, youโ€™re ready to go!


๐Ÿ“š Step 11: Next Steps


๐Ÿ†˜ Step 12: Need Help?

If youโ€™re still having issues:

  1. Check the browser console for error messages
  2. Verify all file paths are correct
  3. Ensure helpers.js is loaded before script.js
  4. Compare your setup with the examples above

Happy coding! ๐Ÿš€