JSFiddle - React, Tailwind, and code Playground

by mpriatel

JavaScript

var expectedNames = [ "mark" , "mike" , "john", "paul" ]
var names = ["mark" , "mike" , "john", "paul" , "jimmy","bob"]

console.log("result", arrayContains( names , expectedNames ) )

function arrayContains( names, expectedNames )
{
    // go through each of your expected elements...
    // ----------------------------------------------
    for( var i = 0 ; i < expectedNames.length; i++ )
        
        // flag to keep track if we found the name.  we set the default value to false
        // ------------------------------------------------------------------------------
        var found = false 
        var expected = expectedNames[i]
        
        // go through each of the elements in the list you want to test
        // -------------------------------------------------------------
        for( var j = 0 ; j < names.length; j++ )
        {
            var name = names[j];
            
            // if the current element matches the current expected name we can stop looping
            // over the inner array.  We set found to 'true'
            // -----------------------------------------------------------------------------
            if ( expected == name )
            {
                found = true
                break;
            }
        }
        // after we have finished the loop above we check to see if we found the element. 
        // if we haven't, there is no need to continue searching through the array we have
        // already proved that a single expectedName is not in in names.
        // ------------------------------------------------------------------------------
        
        if ( !found ){
            return false
        }
    }
    
    // if we have arrived at this point it means we've gone through each expected name
    // and found it in the names array.  
    // --------------------------------------------------------------------------------   

    return true;
}