Back to State Challenge
OOP Studio Ages 9-15

Object Collections: Robo-Pet Roll Call

Put several objects in one array, then use one loop to command every pet.

15-20 minAges 9-15OOPJavaScript
CollectionAn array holds many objects.
LoopVisits each object once.
MethodEach pet runs report().
What are you learning?

The three robo pets are separate objects stored together inside the pets array.

What is a loop for?

A loop repeats code for every item. One loop can command all the pets without writing Pixel, Bolt, and Zippy three separate times.

Where are loops used?

Games use loops for enemies, players, inventory items, projectiles, cards, pets, and anything else stored in a collection.

JavaScript Collection

One array. Three robo pets.

let pets = [3 objects];
What happens? The loop picks the next pet and calls the same method.
NEXT STEPRun the report() loop.

Choose a team command

Press Run and watch the loop visit every array slot.

pets Array

The loop is ready.

pets[0]Pixel the robo cat Pixelenergy: 3
pets[1]Bolt the robo dog Boltenergy: 5
pets[2]Zippy the robo rabbit Zippyenergy: 4
petwaiting for the loop

Roll call results appear here.