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
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.

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.
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.
Your check results
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.
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.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
The original helper’s sentence looks correct once, but calling pop has already shortened favoriteToys. This version preserves the data and gives the same result on repeated calls. slice is introduced here as a non-mutating copy of a selected range. The full Buddy sentence uses the provided three-toy profile; its wording would need a separate choice for a pet with no toys.
function convertArrayToSentence(items) {
if (items.length === 0) return "";
if (items.length === 1) return items[0];
if (items.length === 2) return items[0] + " and " + items[1];
return items.slice(0, -1).join(", ") + ", and " + items[items.length - 1];
}
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(", "));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

7 days freeTry The Code Zone
THE CODE ZONE COMMUNITY
Your next step is easier together.
Try the community free for 7 days.
No charge during your trial. Choose your plan on Skool.
- Ask questions and get support in the community
- Watch every course video walkthrough
- Find a study buddy in the 24/7 study room
Make the most of your trial with Premium.
Choose Premium on Skool to try Thursday live help sessions. Bring your questions, work through code, and get unstuck together.
Scheduled Thursdays in Eastern Time. Check the Skool calendar for your local date and time.
Have a website account? Sign in