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

103
Views
String Match challenge in Javascript

if anybody can point or just give a clue what I did wrong, would be very much appreciated. So the task is :

Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx" "xx", "aa", and "az" substrings appear in the same place in both strings.

function('xxcaazz', 'xxbaaz') should return 3

function('abc', 'abc') should return 2

function('abc', 'axc') should return 0

My code:

function stringMatch(a, b){

//  convert both strings to arrays with split method

  let arrA = a.split("")
  let arrB = b.split("")

//  create 2 empty arrays to feel in with symbol combinations

  let arrOne = [];
  let arrTwo = [];

// loop through the first array arrA and push elements to empty arrayOne

  for ( let i = 0; i < arrA.length ; i++ ) {
      arrOne.push(arrA[i]+arrA[i+1])
  }
// loop through the first array arrB and push elements to empty arrayTwo

  for ( let i = 0; i < arrB.length ; i++ ) {
      arrTwo.push(arrB[i]+arrB[i+1])
  }
//  create a new array of the matching elements from arrOne and arrTwo

  let newArray = arrOne.filter(value => arrTwo.includes(value))

//   return the length 0f the newArray - that's supposed to be the answer

  return newArray.length
}

Thanks for help!

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

0

On the last iteration of your loops, there won't be "next" character, arrB[i+1] will be undefined. The easiest way to solve that is to only loop until the second to last character, or until i < arrB.length - 1.

for ( let i = 0; i < arrB.length - 1; i++ ) {
      arrTwo.push(arrB[i]+arrB[i+1])
  }

e.g...

console.log(stringMatch('xxcaazz', 'xxbaaz')); //should return 3
console.log(stringMatch('abc', 'abc')); // should return 2
console.log(stringMatch('abc', 'axc')); //should return 0

function stringMatch(a, b){

//  convert both strings to arrays with split method

  let arrA = a.split("")
  let arrB = b.split("")

//  create 2 empty arrays to feel in with symbol combinations

  let arrOne = [];
  let arrTwo = [];

// loop through the first array arrA and push elements to empty arrayOne

  for ( let i = 0; i < arrA.length -1 ; i++ ) {
      arrOne.push(arrA[i]+arrA[i+1])
  }
// loop through the first array arrB and push elements to empty arrayTwo

  for ( let i = 0; i < arrB.length - 1; i++ ) {
      arrTwo.push(arrB[i]+arrB[i+1])
  }
  
//  create a new array of the matching elements from arrOne and arrTwo

  let newArray = arrOne.filter(value => arrTwo.includes(value))

//   return the length 0f the newArray - that's supposed to be the answer

  return newArray.length
}

As a bonus, here's my own solution...

console.log(stringMatch('xxcaazz', 'xxbaaz')); //should return 3
console.log(stringMatch('abc', 'abc')); // should return 2
console.log(stringMatch('abc', 'axc')); //should return 0

function stringMatch(a, b){
  var matches = 0;
  for(let i=a.length-1; i--;){
    let s1 = a.substring(i, i+2);
    let s2 = b.substring(i, i+2);
    if(s1 == s2) matches++;
  }
  return matches;
}

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!