Consider the following function, that is supposed to return true only if provided two arrays a and b that are the same.
function arrayEqual(a, b) { for (let i = 0; i < a.length && i < b.length; ++i) { if(a[i] !== b[i]) { return false; } } return true; }
Give three pairs of arrays for which the function correctly returns false, true and then incorrectly return true .
False
Pair 1.) a = , b =
Pair 2.) a = , b =
Pair 3.) a = , b =
True
Pair 1.) a = , b =
Pair 2.) a = , b =
Pair 3.) a = , b =
Incorrectly True
Pair 1.) a = , b =
Pair 2.) a = , b =
Pair 3.) a = , b =
FALSE:
Pair 1)a=[1,5,3] b=[1,2,3]
Pair 2)a=[45,6,56] b=[4,5,6]
Pair 3)a=[2,1,2,3] b=[1,2,3]
it return false for the above pair of arrays as the array numbers are not equal
TRUE:
Pair 1)a=[1,2,3] b=[1,2,3]
pair 2)a=[4,5,6] b=[4,5,6]
pair 3)a=[0,0,0,0]= b=[0,0,0,0]
it returns true for the above pair of arrays as the array numbers and even size of the array is equal
Incorrectly true:
pair a=[1,2,3] b=[1,2,3,4,5]
pair a=[3,4,5,6,7] b=[3,4,5,6]
pair a=[1] b=[1,2,3,4]
it returns true for the above pair of arrays as the array elements are equal but it is incorrect as the size of the arrays are not equal
Get Answers For Free
Most questions answered within 1 hours.