Home > Mobile >  How do I return a value of true if the letters 'a' and 'b' are exactly 3 places
How do I return a value of true if the letters 'a' and 'b' are exactly 3 places

Time:01-21

I'm trying to create a function that takes in a string and outputs a value of true if any of the 'a's in the string are exactly 3 places from the 'b's in the same string. If the 'a's and 'b's are not exactly 3 places from one another than return false.

Examples Input: "after badly" Output: false Input: "Laura sobs" Output: true

This is what I have so far but it doesn't seem to be working. If anyone could take a look and show me where I've gone wrong that would be awesome.

function string(str) {    
    for (i = 0; i < str.length; i  ) {
      if(str[i] === 'a' && str[i 3] === 'b'){
        return true;
      }
    }
    return false;
}
console.log(string('lane borrowed'))

CodePudding user response:

Start by thinking through and describing the problem to yourself. Maybe even re-word it. Below is the logic of the code, with some hints on how to implement it.

  1. Locate the first "a" in the string. Hint: Use the indexOf() method.
  2. If you are more than 3 characters from the end of the string, check the third position after the position you got in 1. above. Hint: Use the substring() method, e.g. myString.substring(pos,1).
  3. If the character you get in 2. is not "b", and you are not at the end of the string, repeat step 1-3 again. Hint: Use do ... while()

CodePudding user response:

You will only get the third element based on your code. Since i 3 get the third element from the i which should be two space apart not three space.

function string(str) {    
    for (i = 0; i < str.length; i  ) {
      if(str[i] === 'a' && str[i 4] === 'b'){
        return true;
      }
    }
    return false;
}
console.log(string('lane borrowed'))

CodePudding user response:

Exactly 3 characters after a in lane borrowed is a space . Did you intend to remove whitespace or did you just miscount? Here's a version of your function which removes the whitespace and it does correctly return true.

str = str.replace(/\s/g, "");

function string(str) {   
    str = str.replace(/\s/g, "");
    for (i = 0; i < str.length; i  ) {
      if(str[i] === 'a' && str[i 3] === 'b'){
        return true;
      }
    }
    return false;
}
console.log(string('lane borrowed'))
console.log(string('lane            borrowed'))

CodePudding user response:

If you want the letter to be 3 letters apart, you need to add 4 to the first index:

function string(str) {
  for (var i = 0; i < str.length; i  ) {
    if(str[i] === 'a' && str[i 4] === 'b'){
      return true;
    }
  }
  return false;
}
console.log(string('a123b'))

CodePudding user response:

You make a good job, but you not remove the blank spaces, so in this case: 'lane borrowed' str[i] = 'a' str[i 3] = ' '

only remove the spaces before:

function string(str) {   
  const strWithouBlankSpaces = str.replace(' ', '')

  for (i = 0; i < strWithouBlankSpaces.length; i  ) {
    if(strWithouBlankSpaces[i] === 'a' && strWithouBlankSpaces[i 3] === 'b'){
      return true;
    }
  }

  return false;
}

console.log(string('lane borrowed'))

CodePudding user response:

Thanks to the existence of Array.prototype.some one just needs to focus on implementing the test/condition for a valid character sequence. some immediately stops iterating the array of character items and returns the true boolean value as soon as the condition matches just once ...

function isCorrectCharacterSequence(char, idx, arr) {
  return (
    (char === 'a')
    && (idx < arr.length - 4)
    && (arr[idx   1] !== 'b')
    && (arr[idx   2] !== 'b')
    && (arr[idx   3] !== 'b')
    && (arr[idx   4] === 'b')
  );
}

console.log(
  'is correct sequence ... "lane borrowed" ?..',
  Array
    .from('lane borrowed')
    .some(isCorrectCharacterSequence)
);
console.log(
  'is correct sequence ... "laneborrowed" ?..',
  Array
    .from('laneborrowed')
    .some(isCorrectCharacterSequence)
);
console.log('\n');

console.log(
  'is correct sequence ... "Laura sobs" ?..',
  Array
    .from('Laura sobs')
    .some(isCorrectCharacterSequence)
);
console.log(
  'is correct sequence ... "after badly" ?..',
  Array
    .from('after badly')
    .some(isCorrectCharacterSequence)
);
console.log('\n');

console.log(
  'is correct sequence ... "arrrbitrary" ?..',
  Array
    .from('arrrbitrary')
    .some(isCorrectCharacterSequence)
);
console.log(
  'is correct sequence ... "arrbbitrary" ?..',
  Array
    .from('arrbbitrary')
    .some(isCorrectCharacterSequence)
);
console.log(
  'is correct sequence ... "arbrbitrary" ?..',
  Array
    .from('arbrbitrary')
    .some(isCorrectCharacterSequence)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

function string(str) {
  for (i = 0; i < str.length - 3; i  ) {
    if (str[i] === 'a' && str[i   3] === 'b') {
      return true;
    }
  }

  return false;
}

console.log(string('lane borrowed'));

  •  Tags:  
  • Related