hendryqiang
hendryqiang

Reputation: 19

Need Help Array for loop If else statement

need help with For loop array , i want the opposite data of function, but when i try arr[i] != elem it print out all the array; and if i try arr[i] == elem , it give me the array that i don't want; still don't understand why it not work with != (not equal).

function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for(let i = arr.length -1; i >= 0 ; i--) {
  for(let j = arr[i].length-1;j >= 0;j--) {
    if(arr[i][j] !== elem) {
        newArr.push(arr[i]);
     }
   }
 }
 // change code above this line
  return newArr;
}

console.log(filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2));

the result i want is ["flutes", 4] sorry if this have been asking by others, i have been looking for the answer trough google , but can not find it.

thanks for the help

Upvotes: 1

Views: 63

Answers (2)

user3297291
user3297291

Reputation: 23397

You were nearly there!

You were checking against every value in the array. I.e.: 2 !== "trumpets" and 2 !== 2. Therefore, all arrays got pushed, because all arrays had a string value that will never match.

Remove the second loop and check for the second value of the inner array explicitly:

function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for(let i = arr.length -1; i >= 0 ; i--) {
    if(arr[i][1] !== elem) {
        newArr.push(arr[i]);
     }
 }
 // change code above this line
  return newArr;
}

console.log(filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2));

The snippet above shows the error in your current approach, but doesn't provide a very re-usable function. If you want to check whether an array includes a value, it's easiest to fall back to some of the built in array methods:

function filteredArray(arr, elem) {
  return arr.filter(values => !values.includes(elem));
}

console.log(filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2));

console.log(filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter"));

Upvotes: 1

Dipak
Dipak

Reputation: 6970

function filteredArray(arr, elem) {
  var resultArray = arr.filter(function(item) {
    return item.indexOf(elem) == -1;
  });
  return resultArray[0]
}
console.log(
  filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2)
);
console.log(
  filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")
);

This code snippet will return the first item in the input array which is not matching with the given second param to the function. If we need all the items which are not matching, replace return resultArray[0] with return resultArray.

Consider a case where we are calling this function with following parameter,

filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 5]], 2)

return resultArray[0] will give ["flutes", 4]

If we need all the items which are not matching we can do

return resultArray will give [["flutes", 4], ["saxophones", 5]]

Upvotes: 0

Related Questions