JavaScript: Your First Code Runner

JavaScript makes websites and games interactive. Today you will experiment with small pieces of code, print what they do, and see how objects can describe a game character.

Objects → Game Data → GameBuilder → Games, Projects, Systems, and Research

1. Reference Guide

Term Meaning Example
Object literal Related information grouped inside { }. const player = { name: "Alex", health: 100 };
Property A key and its value, separated by a colon. health: 100
String / number Text in quotes / a numeric value. "Alex" / 100
Dot notation Reads or changes a property. player.health
console.log Prints a value in the Code Runner output. console.log(player.health);
Nested object An object stored as a property of another object. position: { x: 10, y: 20 }

Separate properties with commas. const keeps the name bound to the same object; you can still change its properties. A JavaScript object literal is related to JSON, but it is not the same as JSON text.

2. LxD Cycle

Define: A new programmer needs a safe way to try code and see what it does.

Ideate: How might we describe a person or game character with a few properties and inspect the results?

Prototype & Test: Edit the examples, predict their output, run them, and use the output or any error message to improve your code.

3. Foundation Skills

This first lesson introduces running JavaScript, grouping information into objects, reading and changing properties, and inspecting output. Later lessons explore variables, arrays, JSON, and the GameBuilder in more depth.

4. Lesson Plan

Learning Objective: Create and modify object literals and use console.log to inspect their values.

Success Criteria: You can identify a key and value, read and change properties with dot notation, run a JavaScript Code Runner, and explain what its output shows.

Using a Code Runner

  1. Read the challenge and predict what the code will print.
  2. Run the code and compare the output with your prediction. Edit the code and run it again.
  3. If you see an error, check quotes, commas, braces, and property names. Correct it and try again.
  4. Save each completed runner and select it for submission; running it alone does not save or submit it.

Use fictional information in every example. You do not need to share personal information.

5. Code Examples and Practice

The OCS GameBuilder uses structured data to configure objects in a 2D game engine. First, practice with a small person object; then use related records and a game character.

Popcorn Hack 1: Person Object

Run the starter code. Change the fictional name and age, add a hobby property, and print it using person.hobby. The existing code also adds and prints school. Save and select this runner for submission.

Code Runner Challenge

Popcorn Hack 1 - Change the fictional name and age, add a hobby, and print it with person.hobby.

View IPYNB Source
%%js

// CODE_RUNNER: Popcorn Hack 1 - Change the fictional name and age, add a hobby, and print it with person.hobby.

// Simple person object with key-value pairs
const person = {
    name: "Jane",
    age: 16,
    grade: "11th",
    favoriteSubject: "Computer Science"
};

// Access values using keys
console.log("Name:", person.name);
console.log("Age:", person.age);
console.log("Favorite Subject:", person.favoriteSubject);

// Add a new key-value pair
person.school = "Example High";
console.log("After adding school:");
console.log(person);

// TODO: Add person.hobby and print it with console.log.
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

infoDb groups records in memory; it is not a persistent database. The first record is nested inside it. Add a second fictional record at infoDb.id_0002 with a name and a hobby. Print both names using dot notation. Save and select this runner for submission.

Code Runner Challenge

Popcorn Hack 2 - Add infoDb.id_0002 with a fictional name and hobby; print both names with dot notation.

View IPYNB Source
%%js

// CODE_RUNNER: Popcorn Hack 2 - Add infoDb.id_0002 with a fictional name and hobby; print both names with dot notation.

const infoDb = {};
infoDb.id_0001 = {
    name: "Sam",
    hobby: "drawing"
};

// TODO: Add infoDb.id_0002 with a name and hobby.
console.log("First name:", infoDb.id_0001.name);
// TODO: Print the second name using infoDb.id_0002.name.
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

6. Hacks & Practice Tasks

Homework Hack: Game Character Data

This simplified playerData is inspired by the configuration used by GameBuilder. It describes data; running this example prints it, but does not create a playable game.

  1. Change the player’s id and SCALE_FACTOR to values of your choice.
  2. Change both x and y inside INIT_POSITION to numbers of your choice.
  3. Add a color string property and print it using dot notation.
  4. Print the player’s ID, scale, and both position values using console.log and dot notation. Check that the output matches your code.
  5. In code comments, identify one key and its value, and explain what console.log showed after your changes.

Save and select all three Code Runners, then use Submit Assignment. The saved code and comments in the selected runners are the evidence for grading. No GameBuilder changes are required for this assignment.

Code Runner Challenge

Homework Hack - Change id, SCALE_FACTOR, and both INIT_POSITION values; add a color string; log each value with dot notation; in comments identify a key and value and explain the output.

View IPYNB Source
%%js

// CODE_RUNNER: Homework Hack - Change id, SCALE_FACTOR, and both INIT_POSITION values; add a color string; log each value with dot notation; in comments identify a key and value and explain the output.

const playerData = {
    id: "playerData",
    SCALE_FACTOR: 5,
    INIT_POSITION: { x: 100, y: 300 }
};

console.log("Player:", playerData.id);
// TODO: Print scale, x, y, and the color property you add.
// TODO: Identify a key and value; explain what your output showed.

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Grading Plan (1.0 Point Total)

The submitted code and comments are the grading evidence. Any fictional names, hobbies, colors, and valid numeric values are acceptable; exact wording is not required.

  • 0.2: Popcorn Hack 1 changes the name and age, adds a hobby, and logs the hobby.
  • 0.2: Popcorn Hack 2 adds a second record with a name and hobby and logs both names.
  • 0.2: Homework changes the ID, scale, and both position values and adds a color string.
  • 0.2: Homework logs the ID, scale, both position values, and color using property access; the code runs without errors.
  • 0.2: Homework comments identify a key and its value and explain what the output showed.

7. Summary and Next Step

Objects organize data; console.log helps you inspect it. GameBuilder uses more detailed data to create playable game objects. The Game Runner below is storage for later GameBuilder work and is not part of this assignment.

8. References

Submit Assignment

Choose what to hand in: click Add to submission on each runner you want graded (hacks, popcorn hacks), and save it. Leaving out practice runners does not lower your grade, but a required hack you leave out is graded as missing. Your selection is sent as a Gist for automatic review.

    Need to update a submission later? Open the submissions dashboard.