catch22
catch22

Reputation: 1693

if else inside double for loop returns undefined

I have two arrays I want to compare, and only add values of one array to the other if they don't already exist in the first one.

I wrote this function:

class Menu {
    constructor(option, name, ingredients) {
        this.option = option;
        this.name = name;
        this.ingredients = [];
    }

    addIngredient(...ingredient) {
      for (var i = 0; i < this.ingredients.length; i++) {
        for (var j = 0; ingredient.length; j++) {
          if (this.ingredients[i] == ingredient[j]) {
            console.log(`The reciple already includes ${ingredient[j]}`);
          } else {
            this.ingredients = this.ingredients.push(ingredient[j]);
            console.log(`${ingredient[j]} has been added`);
          }
        }
      }
    }
 }

The first part of the if statement is working correctly, but when it's the second case, it returns undefined. What am I doing wrong?

Upvotes: 2

Views: 72

Answers (3)

Mouser
Mouser

Reputation: 13304

You need to check if the element exists. No for loop needed here, indexOf will work. Then push your entry. push doesn't return an array.

class Menu {
  constructor(option, name, ingredients) {
    this.option = option;
    this.name = name;
    this.ingredients = [];
  }

  addIngredient(...ingredient) {
    for (var i = 0; i < ingredient.length; i++) {
      if (this.ingredients.indexOf(ingredient[i]) == -1) {
        this.ingredients.push(ingredient[i]);
        console.log(`${ingredient[i]} has been added`);
      } else {
        console.log(`The reciple already includes ${ingredient[i]}`);
        
      }
    }
  }

}

//lets test
var a = new Menu("Test", "test");
a.addIngredient("a", "b", "c", "a");

With Andrea's function:

class Menu {
  constructor(option, name, ingredients) {
    this.option = option;
    this.name = name;
    this.ingredients = [];
  }

  addIngredient(...ingredient) {
    mergeArrays(this.ingredients, ingredient);
  }

}

function mergeArrays(arr1, arr2) {
    arr2.forEach((el) => {
        if (arr1.indexOf(el) == -1) {
            arr1.push(el);
        }
        else
        {
          console.log(`"${el}" already exists in the array.`) //debugging only
        }
    })
}

//lets test
var a = new Menu("Test", "test");
a.addIngredient("a", "b", "c", "a");

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222592

Probably you have not intialized this.ingredients

this.ingredients : any = [];

also before you push elements , check if its empty or not. also you are missing ] in console.log

 for (var j = 0; ingredient.length; j++) {
          if (this.ingredients[i] == ingredient[j]) {
            console.log(`The reciple already includes ${ingredient[j]}`);
          } else {
            this.ingredients.push(ingredient[j]);
            console.log(`${ingredient[j]} has been added`);
          }
        }

EDIT

Replace the line,

this.ingredients = this.ingredients.push(ingredient[j]);

with

this.ingredients.push(ingredient[j]);

Upvotes: 1

Andrea
Andrea

Reputation: 566

function mergeArrays(arr1, arr2) {
    arr2.forEach(function(el){
        if (arr1.indexOf(el) == -1) {
            arr1.push(el);
        }
    })
}

function modifies arr1

Upvotes: 2

Related Questions