skip to Main Content

I need to create a function, lettersAfter(), that accepts three arguments:

  • haystack: The string to be searched
  • needle: A character to search for.
  • limit: The number of letters after the character to return

lettersAfter() should return the letters following the first found occurrence of the search character. The letters returned should be limited to the number of the third argument.

For example:

lettersAfter('hello', 'e', 2); // => 'll'
lettersAfter('hello', 'h', 1); // => 'e'
lettersAfter('indefatigable', 'a', 4); // => 'tiga'

This is my code:

function lettersAfter(haystack, needle, limit) {
  let letters = ''
  for (i = 0; i < haystack.length; i++) {
    if (needle === haystack[i]) {
      for (j = 0; j < limit; j++) {
        letters += haystack[i + j + 1];
      }
    }
  }
  return letters;
}

console.log(lettersAfter('Bobthebuilder', 'b', 5));

According to the prompt, the code should return 'thebu', but it ends up returning 'thebuuilde'. Any advice?

2

Answers


  1. The issue is that after the character is found the first time, the loop keeps going and will add more to letters if the same character is found a second time. To fix this, break out of the loop after you find it the first time:

    function lettersAfter(haystack, needle, limit) {
      let letters = '';
      for (let i = 0; i < haystack.length; i++) {
        if (needle === haystack[i]) {
          for (let j = 0; j < limit; j++) {
            letters += haystack[i + j + 1];
          }
          break; // break out of the loop after first occurance is found
        }
      }
      return letters;
    }
    
    console.log(lettersAfter('Bobthebuilder', 'b', 5));

    However, in the real world, it’s better to just use the built-in functions .indexOf() and .substring(). To replicate returning an empty string if there is no match, just check if .indexOf() returns -1:

    function lettersAfter(haystack, needle, limit) {
      const index = haystack.indexOf(needle);
      if (index == -1) return '';
      return haystack.substring(index+1, index+limit+1);
    }
    
    console.log(lettersAfter('Bobthebuilder', 'b', 5));
    Login or Signup to reply.
  2. Another option is to use indexOf to find your ‘needle’ and use the found index (if there is one) and the limit to determine a substring to return. In the code below, indexOf is used to find the index and 1 is added to this index because we want the letters after needle. Then we check to see if firstIndex > 0 because indexOf returns -1 if no index was found, but since we added 1 this would now be 0. If the condition is true, take a substring from firstIndex and check if the remaining length is less than the limit. I added a return statement for the condition being false to indicate that no index was found.

    NOTE: If the needle can be more than 1 letter/character, you would need to change +1 to +needle.length when assigning a value to firstIndex.

    function lettersAfter(haystack, needle, limit) {
      let firstIndex;
      if ((firstIndex = haystack.indexOf(needle) + 1) > 0) {
        return haystack.substring(firstIndex, 
          haystack.length - firstIndex < limit 
          ? haystack.length 
          : (firstIndex + limit));
      } else {
        return `Needle '${needle}' could not be found!`;
      }
    }
    
    console.log(lettersAfter('Bobthebuilder', 'b', 5))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search