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 ?
5 Answers 5
The problem its easy. You are comparing in whereIsAlice method always the first array and the second array, then the method always find the value and break de for loop.
function whereIsAlice(names) {
for (var i = 0; i < names.length; i++) {
if (names[i].firstName == "Alice") {
return i;
}
}
// When not found in above loop then return -1, not found!
return -1;
}
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
3 Comments
Some of You mutating new array (array.map) from initial array and doing indexOf.
Others are looping with for and then checking the variable index.
Then why JS community is working to extend language constructions, methods and etc?
I recommend You to dive in MDN better and read about findIndex ?
function whereIsAlice(persons) {
return persons.findIndex(function(person) {
return person.firstName === 'Alice';
});
}
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));
console.log(whereIsAlice(friends2));
With ES6 it's so shorter that I don't see reason to create method:
console.log(friends1.findIndex(friend => friend.firstName === 'Alice'));
console.log(friends2.findIndex(friend => friend.firstName === 'Alice'));
Comments
You can do it like this if you want to write your own loop.
var friends1 = [
{ firstName: 'John', lastName: 'Gaudet' },
{ firstName: 'Lisa', lastName: 'Mcclanahan' },
{ firstName: 'Alice', lastName: 'Vore' },
{ firstName: 'Marine', lastName: 'Salsbury' },
];
var friends2 = [
{ firstName: 'Tim' },
{ firstName: 'Arthur' },
{ firstName: 'Juan' },
];
const whereIsAlice = arr => {
for (let i = 0; i < arr.length; i++) {
if (arr[i].firstName === 'Alice') {
return i;
}
}
return -1;
}
console.log(whereIsAlice(friends1)); //Should be 2
console.log(whereIsAlice(friends2)); // Should be -1
Comments
to simplify your Function you can use the next code
function whereIsAlice(persons) {
return persons.map(function(e) { return e.firstName; }).indexOf('Alice');
}
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
Comments
Rewrite Your function as below, This will work properly in your case :
function whereIsAlice(persons) {
var index= -1;
for(var i=0;i<persons.length;i++)
if(persons[i].firstName==="Alice"){index=i;break;}
return index;
}
whereIsAlicefunction?Array.prototype.indexOfat MDN. YourwhereIsAliceis basically a wrapper around this function.