I have stubborn reindeer who talk in javascript

I am having problems in the level “reindeer tender”.
as I wrote in the header, I code in javascript.

when I run my code, one reindeer goes to the right pen, and then all the other reindeer stay in their places and don’t move.
(one time it went crazy and when I ran the code one sleeping reindeer woke up, moved to another pen and went back to sleep).

here is my code:

var penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ];

var penOccupants = [ null, null, null, null, null ];

// And this array contains our reindeer.
var friends = hero.findFriends();

// Figure out which reindeer are already in their pens.
for (var deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    var reindeer = friends[deerIndex];
    // For each position check if it matches a reindeer.
    for (var penIndex = 0; penIndex < penPositions.length; penIndex++) {
        var penPos = penPositions[penIndex];
        if (penPos.x == reindeer.pos.x && penPos.y == reindeer.pos.y) {
            // Put the reindeer in occupants at penIndex
            penOccupants[penIndex] = reindeer.id;
            // Remove the reindeer from the friends array.
            friends[deerIndex] = null;
            // break out of the inner loop here:

// Assign the remaining reindeer to new positions.
for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    // If the reindeer is null, use continue:
    if (friends[deerIndex] === null) {
    // Look for the first pen with nothing.
    for (var occIndex = 0; occIndex < penOccupants.length; occIndex++) {
        // If there is nothing, the pen is open:
        if (penOccupants[occIndex] === null) {
            // Put the reindeer in the occupants array.
            penOccupants[penIndex] = reindeer.id;
            // Command the reindeer to move to the pen.
            hero.command(reindeer, "move", penPositions[occIndex]);
            // break out early so we don't reassign:

I would appreciate help.
foxfire :fox_face:

Two parts that I can see that need correction and both in the last loop.

  1. You are using the wrong variable for the index.
  2. When you are trying to command the reindeer to tell them to move, you either need to create a new variable that is using the new friends list, or use the index with the friends list. Right now, reindeer is linked to the variable above and will only call that reindeer. You need to go through the list of friends that are still available.
penOccupants[penIndex] = reindeer.id; // change index to match for loop index
hero.command(reindeer, "move", penPositions[occIndex]); // change reindeer to friends list with index.