Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Sleepwalkers Javascript Help Needed

I’ve been working on this level but I can’t seem to get it to work. My hero goes to the first position and then does nothing else. Can someone please assist me?

// Senick's prepared the grid map how to build fences.
var hunter = hero.findNearest(hero.findFriends());
var fenceMap = hunter.getMap();

// This function converts grid into XY coordinates.
function convertCoor(row, col) {
    return {x: 34 + col * 4, y: 26 + row * 4};

// Iterate over fenceMap and build at fence at all 1s.
for(var i = 0; i < fenceMap.length; i++){
    for(var j = 0; j < i.length; j++){
        if (fenceMap[j] == 1 && fenceMap[i] == 1){
            var coor = convertCoor(i, j);
            hero.buildXY("fence", coor.x, coor.y);
// Move back to the village after building the fences.
hero.moveXY(22, 15);

Think about the middle term, and what you want j to be less than. On the first iteration through this for loop, i is going to be 0, so the loop will finish.

        if (fenceMap[j] == 1 && fenceMap[i] == 1){

Remember that fenceMap[j] is an array of numbers, so it won’t equal 1. You want the number within the array that’s within the array to equal 1.

I tried two other variations of this statement including:

j < fenceMap[i].length


j < fenceMap.length

but I get the same result.

Great, the first of those looks good. I edited my post above as I spotted another thing…

Ok I solved it. The first one was right like I thought but I did have to change the second thing you suggested. Thank you for that reminder :slight_smile:

Well done, quick work :D.