0

I'm doing a CodeWarrior.com problem (this one: http://www.codewars.com/dojo/katas/521c2db8ddc89b9b7a0000c1/play/javascript).

I implemented it in python first, and my code works fine, however in Javascript, I cannot get the lines output.push(snail(data)); return flattenArray(this.output); to work. Calling the method in a recursive fashion seems to allow the child method to edit the variables inside the parent method.

How can I prevent this (if this is what's happening)?

Here's my code:

snail = function(data) {
  console.log("snail - " + data);
  this.output = [];
  // Get the top row
  this.output.push(data.splice(0,1));

  if(data.length == 0) {
    return this.output;
  }

  // Get the right row
  for(var layer=0;layer<data.length;layer++) {
    this.output.push(data[layer].splice(data[layer].length - 1,1));
  }

  // Get the bottom row
  this.output.push(data.splice(data.length - 1,1));

  // Get the left row
  for(var layer=0;layer<data.length;layer++) {
    this.output.push(data[layer].splice(0,1));
  }

  if(data.length!=0) {
    output.push(snail(data));
    return flattenArray(this.output);
  } else {
    return this.output;
  }
}

function flattenArray(arr) {
  var r = [];
  while (!arrayEqual(r, arr)) {
    r = arr;
    arr = [].concat.apply([], arr);
  }
  return arr;
}

function arrayEqual(a, b) {
  var i = Math.max(a.length, b.length, 1);
  while(i-- >= 0 && a[i] === b[i]);
    return (i === -2);
  }

function clone(obj) {
  if (null == obj || "object" != typeof obj) return obj;
  var copy = obj.constructor();
  for (var attr in obj) {
    if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
  }
  return copy;
}
6
  • 1
    What does this inside your snail function mean? Should output.push(snail(data)); be this.output.push(snail(data)); I think you have misunderstood what this is here Commented Oct 28, 2013 at 23:47
  • output.push(snail(data)); calls push on a (probably non-existent) global object named output. Since you're returning something with this.output, you probably want this.output.push(snail(data));. Commented Oct 28, 2013 at 23:48
  • I added this.output instead of just output to try and stop this problem. It doesn't seem to affect the running of the program at all. Commented Oct 28, 2013 at 23:48
  • @ToddDavies Be more specific with your comments. Added what to try to stop the problem? this.output instead of output? Is it supposed to be a global? What did you think adding this would do? Hard to help if you don't know what you were trying to do Commented Oct 28, 2013 at 23:49
  • Where is snail() getting called? Please create runnable code in jsfiddle.net Commented Oct 28, 2013 at 23:51

1 Answer 1

1

Your output variable should be local. By using this.output, it's being used as a global (or being shared through an object). You want it on the stack so that your recursion can work

snail = function (data) {
    var output = [];

Change all the calls to this.output to be output

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.