Home > Software design >  Javascript array compare function using filter - not working
Javascript array compare function using filter - not working

Time:01-11

I wrote the following code to answer the question:

Write a function justCoolStuff() that takes in two arrays of strings, and, using the built-in .filter() method, returns an array with the items that are present in both arrays.

I wanted to solve this problem using loops instead of the includes() array method. Am I on the right track? Code returns an empty array filled with empty arrays.

const justCoolStuff = (arrOne,arrTwo) => {
  const sharedWord = [];
  for (i = 0; i < arrOne.length; i  ) {
    for (j = 0; j < arrTwo.length; j  ) {
      sharedWord.push(arrOne.filter(arr => arrOne[i] === arrTwo[j]));
    }
  }
  return sharedWord;
};


const coolStuff = ['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 'pogs', 'my room', 'temporary tattoos'];

const myStuff = [ 'rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 'family-night', 'my room', 'braces', 'the information superhighway']; 


console.log(justCoolStuff(myStuff, coolStuff))

// Should print [ 'fruit-by-the-foot', 'skateboards', 'my room' ]

Also, is there a way to write this correctly using a callback function, making it more understandable/readable?

CodePudding user response:

You are not on the right track.

const justCoolStuff = (arrOne,arrTwo) => {
  //  const sharedWord = []; //you don't need this
  return arrOne.filter(function(item) {
    // you need to return true if item is in arrTwo
    // there are a number of ways in which you could test for this, one being
    // using includes on arrTwo
  });
};

CodePudding user response:

The demo below shows how you can use Array#filter and Array#includes (or Array#indexOf):

const 
    coolStuff = ['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 'pogs', 'my room', 'temporary tattoos'],
    myStuff = [ 'rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 'family-night', 'my room', 'braces', 'the information superhighway'],
    
    justCoolStuff = (arr1, arr2) => arr1.filter(item => arr2.includes(item)); //OR
    //justCoolStuff = (arr1, arr2) => arr1.filter(item => arr2.indexOf(item) > -1);
    
    console.log( justCoolStuff(coolStuff, myStuff) );

CodePudding user response:

Solution without array functions

const justCoolStuff = (arrOne, arrTwo) => {
  const sharedWords = [];
  for (i = 0; i < arrOne.length; i  ) {
    const item = arrOne[i]
    for (j = 0; j < arrTwo.length; j  ) {
      const other = arrTwo[j]
      if (item === other) {
        sharedWords.push(item);
        break
      }
    }
  }
  return sharedWords;
};


const coolStuff = ['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 'pogs', 'my room', 'temporary tattoos'];

const myStuff = ['rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 'family-night', 'my room', 'braces', 'the information superhighway'];


console.log(justCoolStuff(myStuff, coolStuff))

// Should print [ 'fruit-by-the-foot', 'skateboards', 'my room' ]

  • Related