Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Need feedback for AB Check using Javascript
    The following code fails for 4 test cases, however when I run it in my console it looks correct: ABCheck("123advb"); "true" ABCheck("123adzvb"); "false" ABCheck("abccccazzzb"); "false" ABCheck("bzzza"); "false" Here's my code: function ABCheck(str) { // code goes here var trimmedString = (((str.split(" ")).join("")).toLowerCase()).split(""); for (var i = 0; i < trimmedString.length; i++) { if(trimmedString[i] == "a") { if (trimmedString[i + 3] == "b") { return "true"; break; }; } }; return "false"; } // keep this function call here // to see how to enter arguments in JavaScript scroll down ABCheck(readline()); Can anyone do a quick sanity check for me? Thanks!!
    vhalbwachs posted this question on 12/27/13 | javascript, challenge
  • +
  • 4
  • -
  • Basically your code is doing the wrong thing :D Look at my code for this challenge. I would recvomend for coderbyte challenges that you learn about regular expressions. Also your code is only checking if there is an a before a b not the other way round. Bzzza should return true. Check it against my code, I did it in one line of code :)
  • +
  • 1
  • -
  • The first step in your code removes spaces from the string, however these spaces should be considered as characters to be counted, as in the example "lane borrowed" should return true since there are exactly 3 characters (including 1 space) between the a and b. Next problem comes because you are only looking for "a" then 3 characters then "b", but this excludes the case of "b" then 3 characters then "a". Also, when you are looking for the "b", your code looks specifically at trimmedString[i + 3] without considering that there may not actually be sufficient elements left in the array for this to return a value. Regular expressions are definitely your friend here - as MarsallOfSound said, check out other people's solutions for more hints - there are longer and shorter ones ;)
  • +
  • 0
  • -
  • Just finished this challenge myself. Keep in mind there needs to be three characters *between* 'a' and 'b', not simply be three steps away. Can't remember if you need to split the string once you do that, but I'm sure you can debug that easily enough. I definitely had to go through this more than once... more than ten times. Once you get it I suggest comparing your code to others. There are some really elegant solutions out there. Cheers and good luck!
    Log in to write an answer.