"Reindeer Tender" one deer does not move, one sleep move to another place

// This array contains the positions of the pens that we want to put the reindeer in.
var penPositions = [{ "x": 20, "y": 24 }, { "x": 28, "y": 24 }, { "x": 36, "y": 24 }, { "x": 44, "y": 24 }, { "x": 52, "y": 24 }];
// This array is used to track which reindeer have been asssigned to which pen.
var assignments = [null, null, null, null, null];
// And this array contains our reindeer.
var friends = hero.findFriends();

// Figure out which reindeer are already in their penPositions.
for (var deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    var reindeer = friends[deerIndex];

    // Go through each position and see if it matches the reindeer's position.
    for (var posIndex = 0; posIndex < penPositions.length; posIndex++) {
        var penPos = penPositions[posIndex];


        if (penPos.x == reindeer.pos.x && penPos.y == reindeer.pos.y) {
            // Put the reindeer in the assignments array in slot posIndex.
            assignments[posIndex] = reindeer;
            // Remove the reindeer from the friends array.
            reindeer = null;
            // break out of the inner for loop here to avoid confusion.
            break;
        }
    }
}

// Assign the remaining reindeer to new positions.
for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
    // If the reindeer in this array slot is null, skip this and continue to the next one.
    reindeer = friends[deerIndex];
    if (reindeer === null) {
        continue;
    }
    // Look for the first pen with nothing already assigned to it.
    for (posIndex = 0; posIndex < assignments.length; posIndex++) {
        // If there's nothing in this slot of the assignments array, then the pen is open.
        if (assignments[posIndex] === null) {
            // Put the reindeer in the assignments array.
            assignments[posIndex] = reindeer;
            // Command the reindeer to move to the pen position.
            hero.command(reindeer, "move", penPositions[posIndex]);
            // break out of the inner for loop here so we don't reassign the deer.
            break;
        }
    }
}

spend lots of time to check what is wrong with this code, still cannot solve out. plz help me.

1 Like

You are not removing the reindeer from the friends array, just changing the value of the variable reindeer. Need to use friends[deerIndex] = null;

1 Like

thanks a lot, I thought change the value to null also mean removing the reindeer. for example:

[1, 2, 3, 4, 5] remove 2 = [1, 3, 4, 5]
[1, 2, 3, 4, 5] change 2 to value to “null” = [1, null, 3, 4, 5]

am I thinking right?

My first thought on this level was to simply remove the reindeer from spot 2: [1,3,4,5]

But because of the way the remainder of the starter code is set up, you need to replace it with “null” instead.

These are not equivalent.

1 Like

try to work on commanding them all

1 Like