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.06 · Objects / Practice · 6 min

Preserve the pet’s toy list

The first sentence looks fine, but the helper removed a toy from the profile.

Activity 6 of 9 in Objects

All courses

JavaScript Basics / 21.06

Preserve the pet’s toy list

Not started

The first sentence looks fine, but the helper removed a toy from the profile.

More options
Rob writes a separate description for Buddy while ball, frisbee and bone all stay in the toy rack.
Rob’s comic strip · Objects

Read named, nested and array values from an object

Work through 5 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)

5 steps · Saved work · Instant feedback

YOUR TURN

Describe Buddy without losing a toy

Repair convertArrayToSentence(items) so it formats toy names without changing the input array. Keep the supplied pet and both logs. Print Buddy is 5 years old and loves to play with his ball, frisbee, and bone. Then print Toys: ball, frisbee, bone. Use a comma before and for three or more items; use a and b for two, the name itself for one, and empty text for zero.

Expected output
Buddy is 5 years old and loves to play with his ball, frisbee, and bone.
Toys: ball, frisbee, bone

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 convertArrayToSentence(items) {
  const lastItem = items.pop();
  return items.join(", ") + ", and " + lastItem;
}
const pet = {
  typeOfPet: "dog",
  name: "Buddy",
  age: 5,
  color: "brown",
  isHappyPet: true,
  favoriteToys: ["ball", "frisbee", "bone"],
};
console.log(
  pet.name +
    " is " +
    pet.age +
    " years old and loves to play with his " +
    convertArrayToSentence(pet.favoriteToys) +
    ".",
);
console.log("Toys: " + pet.favoriteToys.join(", "));

Need a nudge?

pop changes the source list

The source helper removes the last toy with pop. Read the last name by index instead. slice(0, -1) creates a separate shallow array of all but the last item, so joining it does not shorten the original.

Handle short lists deliberately

Return empty text for zero names, the name for one, and names joined by and for two. For three or more, join the earlier names with comma-space and append comma-space-and plus the last name.

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.