codex-lv2-may-2025

Level Navigation: 1 2 3 4 5 6 7 8 9 10 11 12 Current Level: 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32


đź§Ş Level 13: Model Call - Global Variable

What You’ll Do

Add a global variable to store the AI’s response.

📚 Concepts

What is a Global Variable?

A global variable is a variable that can be accessed from anywhere in your JavaScript code - from any function, any file, or any part of your program. Unlike local variables that only exist inside a specific function, global variables are available throughout your entire application.

Where to put them: Global variables should be declared at the top of your JavaScript file, outside of any functions. This makes them accessible to all the code that comes after them. In your chatbot project, you’ll typically put global variables near the top of your script.js file or in your case your chatbot.js file.

Why they are useful: Global variables are perfect for storing data that multiple functions need to access, like API responses, user input, or application state. For example, you might store the AI’s response in a global variable so that different functions can display it, save it, or process it further. They help you share data between different parts of your program without having to pass the same information through multiple function parameters.

Instructions

Add a global variable for the model reply at the top of your chatbot.js file. (e.g., let botReply = "";).

đź’ˇ Code Hints

Creating variables? You’ll need to declare a variable to store the AI’s response. Check out the Variables section in SNIPPETS.md for variable declaration examples.

âś… Check

Open Chrome DevTools (F12) and go to the Console tab. Type botReply and press Enter. You should see an empty string "" or whatever value you assigned. If you get an error like “botReply is not defined”, check that you declared the variable in your chatbot.js file.


Next: Level 14 - Create Function



Level Navigation: 1 2 3 4 5 6 7 8 9 10 11 12 Current Level: 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32