Seek and Destory. Why isn't my solution accepted

Seek and Destory. Why isn't my solution accepted
0

#1
function isValid(value) {
  if(this === value) {
    return false;
  } else {
    return true;
  }
}
function destroyer(arr) {
  // Remove all the values
  //var list = arr[6];
  for(var i = 1; i < arguments.length; i++) {
  arr = arr.filter(isValid,arguments[i]);
  }
  return arr;
}

destroyer([2, 3, 2, 3], 2, 3);

When I do the different tests, it all checks out but isn’t passing me on.

I changed the if(this === value) to == and it all checks out and passes me. Weird.


#2

it’s because the this context in your function is not a number, but a Number.prototype object.

you can inspect with console.log('This is:', this)

Your function would have worked as:

if(this.valueOf() === value) {


#3

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#4
function isValid(value) {
  if(this === value) {
    return false;
  } else {
    return true;
  }
}

There’s really never any need to do this. If statements already take in a boolean, why would you have them return one?

return this !== value;

You should also console.log arguments and see what it looks like. I think your problems are stemming from the fact that arguments works best when it’s modified. You can either a) turn it into an array with Array.from(arguments).slice(1); //this will give you an array of all of the arguments besides arr. or b) write the function like this

function destroyer(arr, ...theArgs){ ..... }

Where theArgs is an array of all of the other arguments after arr.

There’s also an indexOf method. You don’t need to filter each argument out of arr one by one


#5

But the code actually works. I get the results but code camp doesn’t accept the answer. But if I change it to (this == value) code camp accepts it.


#6

When using ===, the function isValid(value) returns true every time no matter what.

The reason has to do with the difference between == (loose comparison) and === (strict comparison). Loose comparison performs type conversion before comparing. Strict does not.

The this value in memory is not a javascript number. it’s a javascript object that works functionally like this (if the value were loosely 10):

this = {
  valueOf: function(){
    return 10 ;
  }
}

So, this == 10 is true, because javascript converts the this object to a number for loose comparison. this ===10 is false, because an object { … } is not exactly equal to the number 10.


#7

But if I try this destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3);

It gives me the correct answer when I do the strict comparison. All the test cases work but code camp doesn’t give me credit for it until I do the loose comparison.


#8

When I enter your code each test case fails.

With the strict comparison it should fail. You’d have to demonstrate what you mean by test cases working. If it were the case maybe you stumbled across a bug but I can’t reproduce it.


#9

Yes, the test cases show they are failing, but the output box contains the expected output. I’m having the same issue with my code.


var argArray = [];
var testArray = [];
var finalArray = [];


function destroyer(arr) {
  // Remove all the values
  testArray = arguments[0];
  for(var i =1; i < arguments.length; i++){
    argArray.push(arguments[i]);
  }
  
  for(var j = 0; j < testArray.length; j++){
    if(argArray.indexOf(testArray[j]) < 0){
      finalArray.push(testArray[j]);
    }
  }
  
  return finalArray;
  

}


#10

@razieltov Ok, I think i see what you’re getting at. The code camp editor is not returning the correct result and I’m not sure why. If you run the code through a normal javascript engine (like a web browser) and inspect the output the === doesn’t work and == does.

For some reason FCC editor isn’t displaying the correct output.

@reichamus Your code is close to correct. The problem is you’re using global variables unnecessarily.

By leaving the var … = [] outside the function call they’re not being reset between tests… so push() keeps adding new values to the arrays which causes the test to fail. You should always keep variables in the most contained scope possible to prevent unintended consequences like this.



function destroyer(arr) {
  
  var argArray = [];
  var testArray = [];
  var finalArray = [];

  // Remove all the values
  testArray = arguments[0];
  for(var i =1; i < arguments.length; i++){
    argArray.push(arguments[i]);
  }
  
  for(var j = 0; j < testArray.length; j++){
    if(argArray.indexOf(testArray[j]) < 0){
      finalArray.push(testArray[j]);
    }
  }
  
  return finalArray;
  
}

destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3);

#11

Awesome! Thanks for your help!!


#12

@cjsheets I see. So its not that === is correct. Its actually incorrect. But for some reason the FCC editor shows the correct output when in reality with that code its different output. I tried the code in a regular webpage and it doesn’t work. I see now. Thank you for that.