Symmetric Differencen can't pass test but works in console - solved

Symmetric Differencen can't pass test but works in console - solved
0

#1

Tell us what’s happening:

Could someone please help me find the error in my code below… When I run in debugger, and with console logs, I am getting correct answers for each test, but they are still marked with a red x when I run in the browser. Thank you in advance!

Your code so far

var current = [];
var gArguments = {};
var index = 0;
var cleanArgs = [];
//main function
function sym(args) {
    cleanArgs = [];
    //if first time running set gArguments
    if(index === 0){
      gArguments = Array.from(arguments);
    }
    //build test array by concatting current to args
    testArray.simplify(args);
    testArray.buildTest(cleanArgs);
    testArray.runTest(testArray.testVals);
    updateArgs(gArguments);
    index++;
    if (gArguments.length === 0){
      current = current.sort(function(a,b){
        return a-b;
      });
      console.log(current);
      return current;
    } else {
      sym(gArguments[0]);
    }
}

//included functions
function updateArgs(x) {
  for (var i = 0; i < x.length - 1; i++) {
    x[i] = x[i + 1];
  }
    gArguments = x.slice(0, x.length-1);
}

//included objects
var testArray = {
  testVals: [],
  simplify: function(x){
    var temp = [];
    for (var i = 0; i <x.length; i++){
      if(cleanArgs.indexOf(x[i]) === -1){
        cleanArgs.push(x[i]);
      }
    }
  },

  buildTest: function(x) {
    this.testVals = current.concat(x);
  },


  runTest: function(x) {
    var testArray = [];
    for (var i = 0; i < x.length; i++) {
      var temp = x.filter(function(val) {
        return x[i] === val;
      });
      testArray.push(temp);
    }

    current = testArray.filter(function(elem){
      return elem.length === 1;
    });
    for(var i = 0; i < current.length; i++){
      current[i] = current[i][0];
    }
  }

};

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:


#2

I found one issue, in the above code, I needed to add a return before calling the function, while this fixed the return issue in console, I am still getting red x’s when the answer is calculated.

for example, when I run sym([1, 2, 3], [5, 2, 1, 4]) in console, it returns [3, 4, 5]. However when I run it in the browser, I still get the error: sym([1, 2, 3], [5, 2, 1, 4]) should return [3, 4, 5].

var current = [];
var gArguments = {};
var index = 0;
var cleanArgs = [];
//main function
function sym(args) {
    cleanArgs = [];
    //if first time running set gArguments
    if(index === 0){
      gArguments = Array.from(arguments);
    }
    //build test array by concatting current to args
    testArray.simplify(args);
    testArray.buildTest(cleanArgs);
    testArray.runTest(testArray.testVals);
    updateArgs(gArguments);
    index++;
    if (gArguments.length === 0){
      current = current.sort(function(a,b){
        return a-b;
      });
      console.log(current);
      return current;
    } else {
     return sym(gArguments[0]);
    }
}

//included functions
function updateArgs(x) {
  for (var i = 0; i &lt; x.length - 1; i++) {
    x[i] = x[i + 1];
  }
    gArguments = x.slice(0, x.length-1);
}

//included objects
var testArray = {
  testVals: [],
  simplify: function(x){
    var temp = [];
    for (var i = 0; i &lt;x.length; i++){
      if(cleanArgs.indexOf(x[i]) === -1){
        cleanArgs.push(x[i]);
      }
    }
  },

  buildTest: function(x) {
    this.testVals = current.concat(x);
  },

  runTest: function(x) {
    var testArray = [];
    for (var i = 0; i &lt; x.length; i++) {
      var temp = x.filter(function(val) {
        return x[i] === val;
      });
      testArray.push(temp);
    }

    current = testArray.filter(function(elem){
      return elem.length === 1;
    });
    for(var i = 0; i &lt; current.length; i++){
      current[i] = current[i][0];
    }
  }

};

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]);

#3

I have tried to update the code to remove all global variables, and use OOP, but it still isn’t working out for me:

//main function
function sym(args) {
  cleanArgs = [];
  //if first time running set gArguments
  if (variables.index === 0) {
    argus.gArguments = Array.from(arguments);
  }
  //reduce args array to have only one of each number present in the array
  testArray.simplify(args);
  // concat simplified args to current symetrical difference 
  testArray.buildTest(testArray.cleanArgs);
  // filters array by each variable and pushes results to a 2d array, then all secondary arrays with length 1 are filtered out.
  testArray.runTest(testArray.testVals);
  //updates arguments to remove the current args
  argus.updateArgs(argus.gArguments);
  //running index just to track for the argus.gArguments array
  variables.index++;
  //test if there are still arguments that need to be ran, if so, re-run function, if not sort current array and return it.
  if (argus.gArguments.length === 0) {
    variables.current = variables.current.sort(function(a, b) {
      return a - b;
    });
    return variables.current;
  } else {
    return sym(argus.gArguments[0]);
  }
}

//included objects
var variables = {
  current: [],
  index: 0
};

var argus = {
  gArguments: {},

  updateArgs: function(x) {
    for (var i = 0; i < x.length - 1; i++) {
      x[i] = x[i + 1];
    }
    this.gArguments = x.slice(0, x.length - 1);
  }
};

var testArray = {
  testVals: [],
  cleanArgs: [],
  simplify: function(x) {
    var temp = [];
    this.cleanArgs = [];
    for (var i = 0; i < x.length; i++) {
      if (this.cleanArgs.indexOf(x[i]) === -1) {
        this.cleanArgs.push(x[i]);
      }
    }
  },

  buildTest: function(x) {
    this.testVals = variables.current.concat(x);
  },

  runTest: function(x) {
    var testArray = [];
    for (var i = 0; i < x.length; i++) {
      var temp = x.filter(function(val) {
        return x[i] === val;
      });
      testArray.push(temp);
    }

    variables.current = testArray.filter(function(elem) {
      return elem.length === 1;
    });
    for (var i = 0; i < variables.current.length; i++) {
      variables.current[i] = variables.current[i][0];
    }
  }

};

sym([
  3, 3, 3, 2, 5
], [
  2, 1, 5, 7
], [
  3, 4, 6, 6
], [
  1, 2, 3
], [
  5, 3, 9, 8
], [1])

#4

You still have the following global variable:

var variables = {
  current: [],
  index: 0
};

If I put a console.log(variables) at the top of your sym function and run multiple test cases in a row, you will see the current property retains it’s value from the last time sym was called.


#5

thanks, still trying to wrap my head around all this :). got it working with a small work around.