Last intermediate algorithm help

Can anyone figure out why the last case test on the last intermediate algorithm won’t work with this solution? I think it has something to do with the arguments.length being only 1 when it should be getting passed 2 arguments ? :frowning:


function addTogether(x, y) {               //outer function
  arr = [];
  for (i in arguments)  {
    arr.push(arguments[i]);
  }
  if (!arr.every(numTest))  {
    console.log("undefined")
    return undefined;
  } else if (arguments.length > 1) {
    console.log(x + y);
    return x + y;
  }

  var sumOfTwo =  function(y) {         //inner function
    console.log(x + y);
    return x + y;
  }

  console.dir(sumOfTwo);
  return sumOfTwo;
}

function numTest(obj)  {
  if (typeof obj === 'number') {
    return true;
  }
  return false;
}

addTogether(2)([3]);

It looks like the parameter for Y is bypassing your outer functions numTest(). The innerFunction is grabbing it and returning the operation making it a string, not a number. The function concatenates 2 & 3 to make “23”

I just figured out a different solution. I added a conditional to my inner function. I gotta try what you said and see which one I like more. Thanks man! :smiley: Here’s what I did btw

function addTogether(x, y) {               //outer function

  console.log(arguments);

  arr = [];
  for (i in arguments)  {
    arr.push(arguments[i]);
  }

  if (!arr.every(numTest))  {
    console.log("undefined")
    return undefined;
  } else if (arguments.length > 1) {
    console.log(x + y);
    return x + y;
  }

  var sumOfTwo =  function(y) {         //inner function
    if (!numTest(y)) {
      console.log(undefined);
      return undefined;
    }
    console.log(x + y);
    return x + y;
  }

  console.dir(sumOfTwo);
  return sumOfTwo;
}

function numTest(obj)  {
  if (typeof obj === 'number') {
    return true;
  }
  return false;
}

Yeah that is what I was working on just now too. Good job!

1 Like

Thanks for your help man :smiley: really encouraging to get feedback to quickly