Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

33
Vistas
unexpected result while using findIndex array method

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.

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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);
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.