2

Hi guys working on a problem in javascript from coder bytes. The problem is:

Using the JavaScript language, have the function ABCheck(str) take the str parameter being passed and return the string true if the characters a and b are separated by exactly 3 places anywhere in the string at least once (ie. "lane borrowed" would result in true because there is exactly three characters between a and b). Otherwise return the string false.

Use the Parameter Testing feature in the box below to test your code with different arguments.

function abcheck(str){
        for(var i = 0; i < str.length; i++) {
            if(str.split(" ")[i] === "a" && str.split(" ")[i+4] === "b")
                {
                    return true;    
                }
            else{
                    return false;
            }
    }
}

After doing a check

abcheck("Laura sobs")

I am getting false. Can anyone spot what I am doing wrong?

3 Answers 3

1

Try this. Used alert to better demonstrate. You were splitting the strings at the space instead of every character like I think you were trying to do.

<script>
str="Laura sobs";

function abcheck(str){
        for(var i = 0; i < str.length; i++) {
            if(str.split("")[i] === "a" && str.split("")[i+4] === "b")
                {
                    alert ("true")    
                }
            else{
                    // do nothing
            }
    }
}

abcheck("Laura sobs")

</script>
Sign up to request clarification or add additional context in comments.

Comments

0

I think the issue you are facing here is the use of the Split function as this will separate your string into an array of words (using a space character as an indication of a new word).

Also, your for loop could encounter issues when it reaches the end of the string, lets say the string was 10 characters long, once the loop reaches its 7th iteration, it will no longer be able to add 4 to the index and check the character in that position, so it would be best to only iterate as far as the 4th from last character, since we already know that there wont be any further possible checks to carry out

Removing both instances of split(" ") from your code, and changing your loop condition slightly should fix the issue you are experiencing, therefore I believe the function should look something like this:

function abcheck(str){
        for(var i = 0; i < str.length - 4; i++) {
            if(str[i] === "a" && str[i+4] === "b")
                {
                    return true;    
                }
            else{
                    return false;
            }
    }
}

Finally, depending on how thorough you need your function to be, you may also need to test for capitalised versions of these letters (i.e. using str.toLowerCase() inside your condition).

1 Comment

This code is wrong actually. I can't figure out why though.
0
function ABCheck(str) {
  if (str.length < 5) {
    return false;
 }
  console.log(str.length);
  for (let i = 0; i < str.length - 4; i++) {
    // look for a
    if (str[i] === 'a') {
      if (str[i + 4] === 'b') {
        return true;
      }
    }

    if (str[i] === 'b') {
      if (str[i + 4] === 'a') {
        return true;
      }
    }
  }

  return false;
}

1 Comment

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

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.