Compare 2 arrays in Javascript
Hi, here is the solution in Javascript for this problem that is commonly asked in interviews.
Problem:
Create a function in Javascript that takes two inputs:
1. – The first array is an array of strings such as: [‘bear’, ‘ant’, ‘viper’, ‘d’];
2. – The second array is an array that defines a desired order of the first letters: [‘a’, ‘b’, ‘c’, ‘e’, ‘z’, ‘d’]
The function should return a Boolean that determines if array #1 follows the order defined by array 2.
First Solution:
// Function that compare the 2 arrays
function arrayComparation(words, letters) {
// Valitations to make sure that arrays are correct
if (words.length <= 0 || letters.length <= 0 || words.length !== letters.length) {
return false;
}
// For to go through every position in the array - this for is not just for words array
for (let i = 0; i < words.length; i++) {
// Comparison if the first letter of words is different to the letter array in that position (i)
if (words[i][0] !== letters[i]) {
// Just printing the first letter and the letter of the second array
console.log('words --->', words[i][0]);
console.log('letter --->', letters[i]);
// returning false
return false;
} else {
// if is true increment
i++;
}
}
// function returning
return true;
}
arrayComparation(['ant', 'boy', 'cristina', 'daniel', 'xove', 'fernando'], ['a', 'b', 'c', 'd', 'e', 'f']);
Result:
Second solution:
// Function that compare the 2 arrays
function arrayComparation(words, letters) {
// Valitations to make sure that arrays are correct
if (words.length <= 0 || letters.length <= 0 || words.length !== letters.length) {
return false;
}
//For to go through every position in the array - this for is not just for words array
for (let i = 0; i < letters.length; i++) {
if (!letters.includes(words[i][0])) {
return false;
} else {
i++;
}
}
return true;
}
console.log(arrayComparation(['ant', 'boy', 'cristina', 'daniel', 'xeve', 'fernando'], ['a', 'b', 'c', 'd', 'e', 'f']));
Result:
Third solution:
// Function that compare the 2 arrays
function arrayComparation(words, letters) {
// Valitations to make sure that arrays are correct
if (words.length <= 0 || letters.length <= 0 || words.length !== letters.length) {
return false;
}
let result = true;
letters.forEach((letter, i) => {
if (!letters.includes(words[i][0])) {
result = false;
} else {
i++;
}
});
return result;
}
console.log(arrayComparation(['ant', 'boy', 'cristina', 'daniel', 'eve', 'fernando'], ['a', 'b', 'c', 'd', 'e', 'f']));
Result:
Four solution:
// Function that compare the 2 arrays
function arrayComparation(words, letters) {
// Valitations to make sure that arrays are correct
if (words.length <= 0 || letters.length <= 0 || words.length !== letters.length) {
return false;
}
let result = true;
letters.forEach((letter, i) => !letters.includes(words[i][0]) ? result = false : i ++)
// function returning
return result;
}
console.log(arrayComparation(['ant', 'boy', 'cristina', 'daniel', 'eve', 'fernando'], ['a', 'b', 'c', 'd', 'e', 'f']));
Result:
By Cristina Rojas.