0

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 ?

4
  • Welcome to Stackoverflow! Commented May 12, 2018 at 10:14
  • Where is your whereIsAlice function? Commented May 12, 2018 at 10:15
  • Check Array.prototype.indexOf at MDN. Your whereIsAlice is basically a wrapper around this function. Commented May 12, 2018 at 10:16
  • as I understand everybody here is new to JS and some of You look in MDN and etc and cannot see simple answer: developer.mozilla.org/ru/docs/Web/JavaScript/Reference/… Commented May 12, 2018 at 10:49

5 Answers 5

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Great i have found my false, i think to complicate !
Happy to help you. Next you can improve the function using other methods, discover its!
I belive that is important learn all possibles ways to implement any algorithm. If he makes this algorithm by this way is important that learn it. If you can read above can see that I recommended explore other better methods or ways to make this
1

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

0

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

0

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

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.