Sleepwalkers - Javascript

CodeCombat - Coding games to learn Python and JavaScript?

My hero builds a single row but needs to build multiple ones. Couldn’t figure out the reason why

// Our sleepwalking peasants are returning.
// But sleeping yetis are also coming.
// DON'T WAKE THEM UP!
// Build fences to let peasants through and stop yetis.
// 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 < fenceMap.length; j++) {
        if (i == 1) {
            var coor = convertCoor(i, j);
            hero.buildXY("fence", coor.x, coor.y);
        }
    }
}
hero.moveXY(22, 15);

Hi, could you use ```javascript instead of just ``` when posting JavaScript code please?

You need to do fenceMap[i][j] because i is just an index and therefore will only be 1 once
And a few tips in JavaScript:

  1. NEVER use var use let instead
  2. Don’t use == much, use === instead
  3. Use for-in loops (for (let i in fenceMap) {) or for-of loops (for (let place of fenceMap) {) when working with arrays

thank you. this worked

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.