I have 2 arrays
First array has firstname and lastname
Second array only has firstname
I will index one firstname and check the arrays
function whereIsAlice(persons) {
var index = 1;
for (var i = 0; i < friends1.length; i++) {
if (friends1[i].firstName === "Alice") {
index = i;
break;
}
if (friends2[i].firstName === "Alice") {
index = i;
}
}
return index
}
var friends1 = [{
firstName: 'John',
lastName: 'Gaudet'
},
{
firstName: 'Lisa',
lastName: 'Mcclanahan'
},
{
firstName: 'Alice',
lastName: 'Vore'
}, // Alice is here, at index 2
{
firstName: 'Marine',
lastName: 'Salsbury'
},
];
var friends2 = [{
firstName: 'Tim'
},
{
firstName: 'Arthur'
},
{
firstName: 'Juan'
},
];
console.log(whereIsAlice(friends1)); //Should be 2
console.log(whereIsAlice(friends2)); // Should be -1
The output are 2 on both. How can i fix it ?
whereIsAlicefunction?Array.prototype.indexOfat MDN. YourwhereIsAliceis basically a wrapper around this function.