Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

634
Views
CodeWars Javascript challenge: IndexOf Array in Array

I'm trying to complete this exercise from codewars. The instructions say: " Write a function that looks for an array within a two-dimensional array and returns the index of the first matching element. If there is no match, your function should return -1"

Exemples:

var arrayToSearch = [[1,2],[3,4],[5,6]];
var query = [1,2]; // => 0
query = [5,6]; // => 2
query = [9,2]; // => -1

This is my solution, but it still fails.

var searchArray = function (arrayToSearch, query) {
  for(i = 0; i < arrayToSearch.length; i++) {
    for (j = 0; j < arrayToSearch[i]; j++) {
      if (arrayToSearch[i][j] === query) {
      return i;
    }
   }
  }
 return -1;
}

I'm not sure what I'm doing wrong, but I think the problem is in the if statement, but I don't know what it is.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Issue in logic.

Your second if condition j < arrayToSearch[i] will always returns false, because you are comparing a number j against an array arrayToSearch[i]. In this case the number j will be compared agaist the first elementin the array arrayToSearch, which makes the function always returns -1.

Corrected Code

var arrayToSearch = [[1, 2], [3, 4], [5, 6]];
var query = [1, 2]; // => 0
// query = [5, 6]; // => 2
// query = [9, 2]; // => -1

var searchArray = function (arrayToSearch, query) {
  for (i = 0; i < arrayToSearch.length; i++) {
    let isEqual = true;
    for (j = 0; j < arrayToSearch[i].length; j++) {
      isEqual = isEqual && (arrayToSearch[i][j] === query[j])
    }
    if (isEqual) {
      return i;
    }
  }
  return -1;
}

console.log(searchArray(arrayToSearch, query));

about 4 years ago · Juan Pablo Isaza Report

0

this simple code will work.

var arrayToSearch = [[1,2],[3,4],[5,6]];
var query = [1,2];
    var searchArray = function (arrayToSearch, query) {
        for (var i = 0; i < arrayToSearch.length; i++) {
            if (arrayToSearch[i][0] == query[0] && arrayToSearch[i][1] == query[1]) {
                return true; 
            }
        }
        return -1;
    }

about 4 years ago · Juan Pablo Isaza Report

0

One-liner if you don't mind:

const arrayToSearch = [[1,2],[3,4],[5,6]];
const query1 = [1,2];
const query2 = [3,4];
const query3 = [5,6];
const query4 = [9,2];

const searchArray = (arr, query) => arr.map(e => e.toString()).indexOf(query.toString());
  
console.log(`[${query1}]:`, searchArray(arrayToSearch, query1));
console.log(`[${query2}]:`, searchArray(arrayToSearch, query2));
console.log(`[${query3}]:`, searchArray(arrayToSearch, query3));
console.log(`[${query4}]:`, searchArray(arrayToSearch, query4));
.as-console-wrapper{min-height: 100%!important; top: 0}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!