JavaScript Program to Compare Elements of Two Arrays Using For Loop Full Project For Beginners

 

 

script.js

 

 

// program to extract value as an array from an array of objects

function compareArrays(arr1, arr2) {

    // check the length
    if(arr1.length != arr2.length) {
        return false;
    } 
    else { 
        let result = false;
        
        // comparing each element of array 
        for(let i=0; i<arr1.length; i++) {

            if(arr1[i] != arr2[i]) {
                return false;
            }
            else {
                result = true;
            }

        }
        return result;
        
  }

}

const array1 = [1, 3, 5, 8];
const array2 = [1, 3, 5, 8];

const result = compareArrays(array1, array2);

// if result is true
if(result) {
    console.log('The arrays have the same elements.');
}
else {
    console.log('The arrays have different elements.');
}

Leave a Reply