0

Below is a code I am using

  const account1 = {
      owner: 'Jonas Schmedtmann',
      movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
      interestRate: 1.2, // %
      pin: 1111,
    };
    
    const account2 = {
      owner: 'Jessica Davis',
      movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
      interestRate: 1.5,
      pin: 2222,
    };
    
    const account3 = {
      owner: 'Steven Thomas Williams',
      movements: [200, -200, 340, -300, -20, 50, 400, -460],
      interestRate: 0.7,
      pin: 3333,
    };
    const account4 = {
      owner: 'Sarah Smith',
      movements: [430, 1000, 700, 50, 90],
      interestRate: 1,
      pin: 4444,
    };
    const accounts = [account1, account2, account3, account4];
    const index = accounts.findIndex(function (acc) {
      acc.pin === 2222;
    });
    console.log(index);

The expected result is 1 but why am I getting -1. I get -1 for whatever condition I put on the findIndex method.

1
  • forgotten to return condition acc.pin === 2222; Commented Jan 15, 2022 at 4:31

1 Answer 1

2

You only need to make the return inside the findIndex function (to return the index where the condition is true). When findIndex no match with some result returns -1.

    const account1 = {
      owner: 'Jonas Schmedtmann',
      movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
      interestRate: 1.2, // %
      pin: 1111,
    };
    
    const account2 = {
      owner: 'Jessica Davis',
      movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
      interestRate: 1.5,
      pin: 2222,
    };
    
    const account3 = {
      owner: 'Steven Thomas Williams',
      movements: [200, -200, 340, -300, -20, 50, 400, -460],
      interestRate: 0.7,
      pin: 3333,
    };
    const account4 = {
      owner: 'Sarah Smith',
      movements: [430, 1000, 700, 50, 90],
      interestRate: 1,
      pin: 4444,
    };
    const accounts = [account1, account2, account3, account4];
    const index = accounts.findIndex(function (acc) {
      return acc.pin === 2222;
    });
    console.log(index);

And I prefer the code like this:

    const accounts = [
        {
          owner: 'Jonas Schmedtmann',
          movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
          interestRate: 1.2, // %
          pin: 1111,
        },
        {
          owner: 'Jessica Davis',
          movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
          interestRate: 1.5,
          pin: 2222,
        },
        {
          owner: 'Steven Thomas Williams',
          movements: [200, -200, 340, -300, -20, 50, 400, -460],
          interestRate: 0.7,
          pin: 3333,
        },
        {
          owner: 'Sarah Smith',
          movements: [430, 1000, 700, 50, 90],
          interestRate: 1,
          pin: 4444,
        }
    ];

    const index = accounts.findIndex(function (acc) {
      return acc.pin === 2222;
    });

    console.log(index);
Sign up to request clarification or add additional context in comments.

2 Comments

You can also add the arrow function implementation of the same with user specified dataset const index = accounts.findIndex((acc) => acc.pin === 2222);
I also prefer the arrow function, thank you!

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.