0

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!

2
  • 1
    How do I ask a good question?: "Describe the problem. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question." + Add a minimal reproducible example with input, expected and actual output. Commented Dec 29, 2021 at 18:18
  • 1
    In this case it's pretty obvious what the problem was and even if it wasn't obvious it would have taken 5 seconds to copy the code and run it yourself, but some people are more interested in criticizing than helping. Commented Dec 29, 2021 at 18:27

1 Answer 1

1

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;
}

Sign up to request clarification or add additional context in comments.

Comments

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.