简体   繁体   中英

I'm stuck with this FreeCodeCamp algorithm

function destroyer(arr) {
    // Remove all the values
    var toFilter;
    var toFrom;
    for(var i=0;i<arguments.length;i++) {
        if (typeof arguments[i]=="object") {
            toFilter = arguments[i];
        } else {
            toFrom = arguments[i];
        }
    }

    var result = toFilter.filter(function(value,index,array) {

        if(value===toFrom) {
            return true;
        }

        return result;

    });

    console.log(result);

}

destroyer([1, 2, 3, 1, 2, 3], 2, 3); // it should return [1,1]

It's almost been more than one week that i'm trying to solve this algorithm. In this task we need to remove the arguments value present in the initial array, they have mentioned to solve this by using arguments and filter method. Well i know what filter and arguments does in Javascript and i solved the previous algorithm using filter method and i read about arguments and it's use but never used it before. Every time i did something using these two it gave me a blank array. Please can anyone help me how can we do this using filter and arguments ?

Just my idea, you can modify it the way you want:

function isValid() {
  return value != 2 && value != 3;
}
var arr = [1, 2, 3, 1, 2, 3];
arr.filter(isValid); //It should return [1,1];

What you need now is to get 2 and 3 value from arguments object and change isValid function.

What you can do is construct a lookup of the values that you want to remove from the array, and then filter the original array by returning values that do not exist in the lookup:

 function destroyer(arr) { var lookup = {}; // For each remaining argument, add it to the lookup. for (var i = 0; i < arguments.length; i++) { lookup[arguments[i]] = true; } // Filter the array and return a new array // with elements that don't exist in the lookup. return arr.filter(function(elem) { return typeof lookup[elem] === 'undefined'; }); } console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3)); //it should return [1,1]

There are several problems, let's list them one by one:

  1. You overwrite toFrom , so instead of your intention to get ALL non-objects (ie 2 & 3), you only get the last non-object (ie 3)
  2. In filter() , return true means "Keep this element in the list" , and you return true if the value is equal to toFrom = 3
  3. The verb solve does not work with the noun algorithm . Because algorithm is the way to solve a problem

I didn't try but I guess your code will output [3,3] , so if you want to output [1,1] , try following modified code based on your version:

 function destroyer(arr) { // Remove all the values var toFilter; var toFrom = []; for(var i=0;i<arguments.length;i++) { if (typeof arguments[i]=="object") { toFilter = arguments[i]; } else { // maintain a list of ALL non-objects which to be filtered toFrom.push(arguments[i]); } } var result = toFilter.filter(function(value,index,array) { if(toFrom.indexOf(value) != -1) { // If element is in the toFrom list, DO NOT keep it return false; } return true; }); console.log(result); } destroyer([1, 2, 3, 1, 2, 3], 2, 3); // it should return

or you may try my version :)

 function destroyer(arr) { // Remove all the values for(var i=0;i<arguments.length;i++) { arr = arr.filter( x => x !== arguments[i]); } console.log(arr); } destroyer([1, 2, 3, 1, 2, 3], 2, 3); // it should return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM