-1

Im trying to determine the location of a in a array. Im not really sure how to handle this case

const expect = require('chai').expect;
const answers = require('../src/arrays');

describe('arrays', function() {
    let a;

    beforeEach(function() {
        a = [ 1, 2, 3, 4 ];
    });

    it('expect determine location of a in array', function(expect) {
        expect(answers.indexOf(a, 3)).to.eql(2);
        expect(answers.indexOf(a, 5)).to.eql(-1);

    });
georg
  • 211,518
  • 52
  • 313
  • 390
Hecako
  • 1
  • 1
  • I’m pretty sure “A” must be an element in “answers”? Your usage here is unclear. What is answers? Why are you looking for an array in “answers”? Is that what you meant to look for? – evolutionxbox Oct 22 '19 at 07:23
  • I have added both simple (*array of numbers*) and complex (*array of objects*) link as dupe. Please do refer both for more clarity – Rajesh Oct 22 '19 at 07:27

1 Answers1

0

Try this!

var array = [ 1, 2, 3, 4 ]; 
   
function finding_index(element) { 
  return element === 2; 
} 
  
console.log(array.findIndex(finding_index));
Yahiya
  • 761
  • 4
  • 15