Search code examples
javascriptif-statementintegercomparison

Javascript Comparison Loop Not Working Between an Integer and an Array of Integers


Based on the code I have below, the user_id_org should match with the first entry of the array in orgs. I had this working without issue but now cannot get it to work, am unsure what I changed, and can't find the issue. When I run the code below, it returns with "No! We did not intersection with 1 of multiple orgs!" when it should return with "Yes! We matched with 1 of multiple orgs!". My goal here is to understand why/what I am doing wrong as to why the numbers are not matching, along with getting a fix.

Additionally, if someone has advice on how I could improve this code, I'd love to hear it.

var orgs = [1234,12345,37463,47716,37463];
var org_id = '';
var user_search = 1;
var org_search = 5;
var user_id_org = 1234;

if (org_id == '') {
    if (user_search == 1) { //User Known
        if (org_search == 1) {
                //Check the single org and see if the org ID is tied to the found user, if so attach user to the correct org.
                //If no intersection, submit a ticket as the user and do (information to be discussed in the meeting)
                var arrayLength = orgs.length;
                for (var i = 0; i < arrayLength; i++) {
                    if (orgs[i] == user_id_org) {
                        var intersection = 'Yes! We matched with 1 org!'
                    } else {
                        var intersection = 'No! We did not intersection with 1 org!'
                    }
                }
                console.log(intersection)
        } else if (org_search > 1){
                //Loop through orgs and see if orgs any intersection the org ID tied to the found user, if so attach user to the correct org.
                var arrayLength = orgs.length;
                for (var i = 0; i < arrayLength; i++) {
                    if (orgs[i] == user_id_org) {
                        var intersection = 'Yes! We matched with 1 of multiple orgs!'
                    } else {
                        var intersection = 'No! We did not intersection with 1 of multiple orgs!'
                    }
                }
                console.log(intersection)
        } else if (org_search == 0) {
                //Create a ticket assigned to the user, and flag it (meeting)
                console.log('We did find a user, but no orgs at all, off to the dedicated org we go.')
        } else {
            console.log('Matching Error')
        }
    } else if (user_search !== 1) {
        if (org_search >= 1) {
            console.log('We did not find a user but found multiple orgs! To the dedicated org we go.')
        } else if (org_search == 1) {
            console.log('We did not find a user but found a single org! Assign them to the org.')
        } else if (org_search == 0) {
            var intersection = 'No intersection because we did not find a user.'

        } else {
            console.log('No User Found Error')
        }
        console.log(intersection)
    } else {
        console.log('Error');
    }
} else if (org_id !== '') {
    if (user_search == 1) {
        var intersection = 'We matched because we had an org ID and found a user.'
    } else if (user_search !== 1) {
        //Create user and attach them to the org_id.
        var intersection = 'We received an org ID but no user, go create them!'
    } else {
        console.log('Org ID Provided Error')
    }
    console.log(intersection)
} else {
    return 'Primary Org ID Sorting Error';
}

Solution

  • The issue is with these lines of code:

    for (var i = 0; i < arrayLength; i++) {
        if (orgs[i] == user_id_org) {
            var intersection = 'Yes! We matched with 1 of multiple orgs!'
        } else {
            var intersection = 'No! We did not intersection with 1 of multiple orgs!'
        }
    }
    

    The for loop continues from 0 to arrayLength - 1. On the 1st iteration (i.e. i is 0), the intersection variable is set to 'Yes!...'. On subsequent iterations, it is overwritten to 'No!...'. You should exit early using the break statement (or return if there's no more code in the function that's relevant).

    for (var i = 0; i < arrayLength; i++) {
        if (orgs[i] == user_id_org) {
            var intersection = 'Yes! We matched with 1 of multiple orgs!'
            break; // 'skip' the remaining iterations
        } else {
            var intersection = 'No! We did not intersection with 1 of multiple orgs!'
        }
    }
    // break jumps here
    

    Other advice,

    • learn to debug your using breakpoints or console.logs.
    • use === instead of == which has unexpected type casting rules.
    • use let and const instead of var
    • end statements with semicolons ;
    • just styling, but most JS avoids using {} around one-line blocks
    • prefer array.forEach to traditional for loops.