Chris Wang Chris Wang - 1 month ago 5
Javascript Question

Check against two arrays using for loop and if statement

I'm trying to run a check against two arrays (one has 4 objects, one just have a few strings) with for loops and if statement for a problem set.

The idea is to use for loop to iterate over every element in the object array and the string array, then use if statement to figure out matches and shove the matching string into a new array. Once all the elements are iterated, it returns the string if there is a matching one.

The problem is the function calls it a day once a single match in the object array is found and returns that only that instead of iterating over the rest of the elements in the object array.



var passengers = [
{ name: ["Michael Jackson"], paid: true },
{ name: ["Osama"], paid: false },
{ name: ["Harambe"], paid: true },
{ name: ["Pepe"], paid: true },
];

var noFlyList = ["Jimmy", "John", "Pepe", "Osama"];

function checkNoFly(passengers, noFlyList) {
for (var i = 0; i < passengers.length; i++) {
for (var j = 0; j < noFlyList.length; j++) {
if (passengers[i].name[0] == noFlyList[j]) {
var passengerList = [];
passengerList.push(passengers[i].name[0]);
return passengerList;
}
}
}
return true;
}

function checkNotPaid(passengers) {
return (!passengers.paid);
}

function processPassenger(passengers, testFunction) {
for (var i = 0; i < passengers.length; i++) {
if (testFunction(passengers[i])) {
return false;
}
}
return true;
}

var allCanFly = processPassenger(passengers, checkNoFly);
if (!allCanFly) {
console.log("We cannot fly because " + checkNoFly(passengers, noFlyList) + " is on the no-fly list");
}

var allPaid = processPassenger(passengers, checkNotPaid);
if (!allPaid) {
console.log("we cannot fly because not all passengers have paid");
}




Answer

use this: having passengerList in the loop make its reinitialized to empty array on each loop, and returning passengerList in the loop makes the loop break once it finishes the first loop

function checkNoFly(passengers, noFlyList) {
    var passengerList = [];
    for (var i = 0; i < passengers.length; i++) {
        for (var j = 0; j < noFlyList.length; j++) {
            if (passengers[i].name[0] == noFlyList[j]) {

                passengerList.push(passengers[i].name[0]);

            }
        }
    }
    return passengerList;
}

EDIT:

change your original processPassenger function to the one below, originally you have only 1 argument passed to your checkNoFly function, where you'd defined it to take 2 arguments, so the false is returned for wrong number of argument, which stop you from getting it the way you want.

function processPassenger(passengers, testFunction) {

    if (testFunction(passengers, noFlyList).length !=0) {
        return false;
    }

    return true;
}

EDIT 2: for your updated question, since for the first check we are returning an array for the single function processPassenger() for validation, we can take similar approach for the checkNotPaid function to return an array for those who have not paid.

function checkNotPaid(passengers) {
    var passengerNotPaid = [];
    for (var i = 0; i < passengers.length; i++) {
        if (!passengers[i].paid) {
            passengerNotPaid.push(passengers[i].name[0]);
        }
    }
    return passengerNotPaid;
}

unless you'd want to refactor everything, i think this would be ok.

Comments