# [SOLVED] Request Assistance with Reindeer Tender

I’ve run out of ideas to try to fix this. When code is run, the three reindeer go to the first three pens, regardless of whether or not one is already occupied.

``````# This is the array of pen positions
penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ]

# Use this array to keep track of each pen's reindeer.
penOccupants = [ None, None, None, None, None, ]

# And this array contains our reindeer.
friends = hero.findFriends()

# Figure out which reindeer are already in their pens.
for deerIndex in range(len(friends)):
reindeer = friends[deerIndex]

# For each position check if it matches a reindeer.
for penIndex in range(len(penPositions)):
penPos = penPositions[penIndex]

if penPos.x == reindeer.pos.x and penPos.y == reindeer.pos.y:
# Put the reindeer in occupants at penIndex
reindeer = penOccupants[penIndex]
# Remove the reindeer from the friends array.
friends[deerIndex] = None
# break out of the inner loop here:
break
pass

# Assign the remaining reindeer to new positions.
for deerIndex in range(len(friends)):
# If the reindeer is null, use continue:
if not friends[deerIndex]:
continue
# Look for the first pen with nothing.
for occIndex in range(len(penOccupants)):
# If there is nothing, the pen is open:
if not penOccupants[occIndex]:
# Put the reindeer in the occupants array.
penOccupants[occIndex] = friends[deerIndex]
# Command the reindeer to move to the pen.
hero.command(penOccupants[occIndex], "move", penPositions[occIndex])
# break out early so we don't reassign:
break
pass
``````

Nevermind. I used hero.say to figure it out. Code wasn’t iterating through the current occupants of the pens.

1 Like

Thanks

I need help with this level too. This is my code:

``````// This is the array of pen positions
var penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ];

// Use this array to keep track of each pen's reindeer.
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
reindeer = penOccupants[penIndex];
// Remove the reindeer from the friends array.
friends[deerIndex] = null;
// break out of the inner loop here:
break;
}
}
}

// Assign the remaining reindeer to new positions.
for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
// If the reindeer is null, use continue:
if (reindeer === null) {
continue;
}
// 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.
reindeer = penOccupants[occIndex];
// Command the reindeer to move to the pen.
hero.command(penOccupants[occIndex], "move", penPositions[occIndex]);
// break out early so we don't reassign:
break;
}
}
}

``````
``````// code
var penOccupants = [ null, null, null, null, null ];
// code
var friends = hero.findFriends();
// code
var reindeer = friends[deerIndex];
// code
// Put the reindeer in occupants at penIndex
reindeer = penOccupants[penIndex];
// you are assigning null to a reindeer - do you need it?

// before the second loop the reindeer is the last friend from the first loop

for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
// If the reindeer is null, use continue:
if (reindeer === null) // you are not iterating through friends but commanding only the last one
// code
// Put the reindeer in the occupants array.
reindeer = penOccupants[occIndex]; // you are doing quite the opposite as in the first loop
``````

Sorry, I don’t get what you are saying:confused:

I’m showing the lines needed to be changed to pass the level:

``````reindeer = penOccupants[penIndex];
// you are assigning null to a reindeer - do you need it?

if (reindeer === null)
// you are not iterating through friends but commanding only the last one

reindeer = penOccupants[occIndex];
// you are doing quite the opposite as in the first loop
``````

You can see other topic about the level - you are not the only one with similar errors.

How are you supposed to remove reindeer from the friends array?

should I just assign it to null?

reindeer = penOccupants[penIndex];
penOccupants[penIndex] is null. so reindeer becomes null.
You must put the reindeer in occupants at penIndex, but you have inverted assignment.

After correcting this line the hero will say the names of two sleeping reindeer:

Thank you for the advice but I still need help. It tells you to remove the reindeer from the friends array.
How do you do that?

This is my code. DO you see anything wrong?

``````// This is the array of pen positions
var penPositions = [ {"x":20,"y":24}, {"x":28,"y":24}, {"x":36,"y":24}, {"x":44,"y":24}, {"x":52,"y":24} ];

// Use this array to keep track of each pen's reindeer.
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;
hero.say(penOccupants);
// Remove the reindeer from the friends array.

// break out of the inner loop here:
break;
}
}
}
// Assign the remaining reindeer to new positions.
for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
// If the reindeer is null, use continue:
if (reindeer === null) {
continue;
}

// 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[occIndex] = reindeer;
// Command the reindeer to move to the pen.
hero.command(reindeer, "move", penPositions[occIndex]);
// break out early so we don't reassign:
break;
}
}
}

``````

You have deleted i line that’s OK:

``````            friends[deerIndex] = null;
``````

and didn’t correct the second error:

``````for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
hero.say(reindeer);
// If the reindeer is null, use continue:
if (reindeer === null) {
continue;
}
``````

Hero is repeating the Vixen name. Why?
One can use hero.say(something) or console.log(something) for debugging.
Another hint:
Your code run at my machine with and debugging with console.log() (Ctrl+shift+I on Chrome opens the console)

``````// your code
for (deerIndex = 0; deerIndex < friends.length; deerIndex++) {
console.log('reindeer = ' + reindeer.id );
if (friends[deerIndex] === null)
console.log('friends[' + deerIndex +'] = null');
else
console.log('friends[' + deerIndex +'] = ' + friends[deerIndex].id);
// If the reindeer is null, use continue:
if (reindeer === null) {
continue;
}