-1

I have this code below

let geese = ["African", "Roman Tufted", "Toulouse", "Pilgrim", "Steinbacher"];
let array = ["Mallard", "Hook Bill", "African", "Crested", "Pilgrim",  "Toulouse", "Blue Swedish"];

function gooseFilter(array, geese) {
     var name = [];
     for (let i = 0; i < geese.length; i++) {
         if (array.includes(geese[i])) {
             name.push(geese[i]);
         }
         return name;    
     }
}

gooseFilter(array);

and I have an error 'Cannot read property 'length' of undefined', and I don't know why. Do you have any idea how can I fix this?

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
  • 2
    `gooseFilter(array,geese );` instead of `gooseFilter(array);` – Sudhir Ojha Apr 03 '19 at 13:01
  • 1
    You'll also need to put the return out of the for loop. Your code code be simplified to `const result = geese.filter(e => array.includes(e))` – baao Apr 03 '19 at 13:02

6 Answers6

3

You forget to pass the second args. And your return statement should be out of loop. consider the following snippet:

let geese = ["African", "Roman Tufted", "Toulouse", "Pilgrim",
    "Steinbacher"
];
let array = ["Mallard", "Hook Bill", "African", "Crested", "Pilgrim",
    "Toulouse", "Blue Swedish"
];

function gooseFilter(array, geese) {
    var names = [];
    for (let i = 0; i < geese.length; i++) {
        if (array.includes(geese[i])) {
            names.push(geese[i])
        }
    }
    return names;
}

console.log(gooseFilter(array, geese));
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
elouassif
  • 308
  • 1
  • 10
2

You declare a variable named geese here:

function gooseFilter(array, geese) {

Then you define it here:

gooseFilter(array);

Since you pass only one argument, the second (geese) is undefined.


If you want to access the geese variable declared in the wider scope, then don't mask it with another variable of the same name.

function gooseFilter(array) {
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Instead of

function gooseFilter(array, geese) {

make it

function gooseFilter(geese) {
mbojko
  • 13,503
  • 1
  • 16
  • 26
0

call gooseFilter(array); with second argument

let geese = ["African", "Roman Tufted", "Toulouse", "Pilgrim", 
"Steinbacher"];
let array = ["Mallard", "Hook Bill", "African", "Crested", "Pilgrim", 
"Toulouse", "Blue Swedish"];

   function gooseFilter(array, geese) {
   var name = [];
   for (let i=0; i<geese.length; i++) {
     if (array.includes(geese[i])) {
     name.push(geese[i])
    }
   return name;    
  }
    }

  gooseFilter(array,geese);
Jatin Parmar
  • 2,759
  • 5
  • 20
  • 31
  • You are looking for [intersection](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) –  Apr 03 '19 at 13:03
0

You defined function gooseFilter which accepts two arguments, but then, when calling it you pass only one param to it: gooseFilter(array).

You have to pass both defined arrays to your function: gooseFilter(array, geese)

robson3999
  • 25
  • 5
0

There are 2 mistakes in your code:

  1. You defined function gooseFilter with 2 params but call with only one array, this cause geese is undefined. That why you got error

'length' of undefined

  1. You return in the for loop. This will make the loop stop after the first item.

The code should be

let geese = ["African", "Roman Tufted", "Toulouse", "Pilgrim", 
"Steinbacher"];
let array = ["Mallard", "Hook Bill", "African", "Crested", "Pilgrim", 
"Toulouse", "Blue Swedish"];

function gooseFilter(_array, _geese) { // parameter # variable
     var name = [];
     for (let i=0; i<_geese.length; i++) {
       if (_array.includes(_geese[i])) {
       name.push(_geese[i])
       }
       // return name;  <-- Return here will stop the loop after the first item
     }
     return name; // return here instead
     
}

const rs = gooseFilter(array, geese) 

console.log(rs)
bird
  • 1,872
  • 1
  • 15
  • 32