thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

21.08 · Objects / Practice · 6 min

Pair each key with its value

The key changes on each iteration. Brackets must use that changing name.

Activity 8 of 9 in Objects

All courses

JavaScript Basics / 21.08

Pair each key with its value

Not started

The key changes on each iteration. Brackets must use that changing name.

More options
Rob pairs keys a, b and c with their values 1, 2 and 3.
Rob’s comic strip · Objects

Read object values through changing keys

Work through 3 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

3 steps · Saved work · Instant feedback

YOUR TURN

Read the value behind each key

Repair printObjectProperties(record) to print each own enumerable property name and its value. Use the key variable in brackets; keep the Object.hasOwn guard and source object a:1, b:2, c:3. Expected lines: a: 1, b: 2, c: 3. Do not hard-code property names or values.

Expected output
a: 1
b: 2
c: 3

Keep the starting inputs. Print these lines in this order, without extra output.

Edit JavaScript, then run it. Check Challenge shows which output checks pass or need a fix. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
function printObjectProperties(record) {
  for (const key in record) {
    if (Object.hasOwn(record, key)) {
      console.log(key + ": " + record.key);
    }
  }
}
printObjectProperties({ a: 1, b: 2, c: 3 });

Need a nudge?

The loop gives you a key string

key changes from a to b to c. record.key reads a property literally named key. record[key] reads the property named by the current key string.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Choose a named property first, then follow the shape of its value.