简体   繁体   English

我被这个 FreeCodeCamp 算法困住了

[英]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.好吧,我知道过滤器和参数在 Javascript 中的作用,我使用过滤器方法解决了以前的算法,我阅读了参数和它的使用,但以前从未使用过。 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.您现在需要的是从arguments对象中获取 2 和 3 值并更改 isValid 函数。

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)您覆盖toFrom ,因此您不是打算获取所有非对象(即 2 和 3),而是只获取最后一个非对象(即 3)
  2. In filter() , return true means "Keep this element in the list" , and you return true if the value is equal to toFrom = 3filter() , return true表示“将此元素保留在列表中” ,如果值等于toFrom = 3则返回true
  3. The verb solve does not work with the noun algorithm .动词solve不适用于名词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:我没有尝试,但我猜你的代码会输出[3,3] ,所以如果你想输出[1,1] ,请尝试根据你的版本修改代码:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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