These are chat archives for Codewars/codewars.com/kata-solving-help

11th
Mar 2017
Steve
@wheelsofsteel
Mar 11 2017 19:45
Hey all, I'm getting type errors from the random test cases for this kata: "https://www.codewars.com/kata/please-help-bob/train/javascript". Basically you add "err" (depending on case) to the end of a word if it has a consonant. I pass the regular tests but get "Cannot read property 'match' of undefined
at errBob" for the random cases (which I can't see what has been passed into the function). the (very messy) code is:
function errBob(string){
  string = string.split(" ");
  var alphabet = /[a-z]/gi;
  var vowels = /[aeiou]/gi;
  for(var i = 0; i < string.length; i ++) {
    string[i] = string[i].split("");

    var toMinus = 1;

    while(!string[i][string[i].length - toMinus].match(alphabet)) {
      toMinus++;
    }


    if(!string[i][string[i].length - toMinus].match(vowels)) {
      if(string[i][string[i].length - toMinus] === string[i][string[i].length - toMinus].toUpperCase())
        string[i][string[i].length - toMinus] += "ERR"; 
      else {
        string[i][string[i].length - toMinus] += "err";
      }
    }

    string[i] = string[i].join("");
  }
  console.log(arguments);
  return string.join(" ");
}
Is it a null value being passed in, maybe? or a single string possibly
itamargal
@itamargal
Mar 11 2017 20:08
@wheelsofsteel This isn't really answering your question as asked but, as far as solving this kata goes, you might want consider abandoning the str.split() method and instead using str.replace() with regular expressions.
Benjamin Kästner
@bkaestner
Mar 11 2017 20:36
I guess that toMinus gets greatr than length.
And I concur with itamargal. Replace makes things a lot easier.
By the way, if there are two spaces right after each other, you will end up with string = ["",""], therefore string[0].length === 0.
And string[0 - 1] is undefined.
Steve
@wheelsofsteel
Mar 11 2017 20:41
Hey again, I'm trying to solve it with replace (as it does seem a lot lot lot less code, now that i'm thinking about it)
function errBob(string) {
  var regex = /[b-df-hj-np-tv-z]\b/gi;
  string.match(regex).forEach(function(ele) {
    if(ele === ele.toUpperCase()) {
      string = string.replace(ele, ele + "ERR");
    } else {
      string = string.replace(ele, ele + "err");
    }
  })

  return string;
}
not passing yet, but feel on the right track
itamargal
@itamargal
Mar 11 2017 20:43
@wheelsofsteel That looks pretty close to what I did. I wouldn't use the case-insensitive option though, since you end up checking the case anyway.
I would also avoid the forEach() - the replace method can handle iterating over the matches by itself.
Just my 2 cents.
Steve
@wheelsofsteel
Mar 11 2017 20:45
Well, I wanted to do something like string.replace(string.match(regex), string.match(regex) += "err") if that makes sense
as in, add "err" to the first arg
but I wasn't sure how to do that, ha
  • "err"*
itamargal
@itamargal
Mar 11 2017 20:46
Yeah, I think that makes sense. I think that using regex capture groups is a little nicer though.
Steve
@wheelsofsteel
Mar 11 2017 20:46
this is one of those challenges that I know can be solved with almost one line, but I just don't know the language deep enough
itamargal
@itamargal
Mar 11 2017 20:47
I was trying to suggest something like this: string.replace(/([bcdfghjklmnpqrstvwxyz])\b/g, "\$1err")
Yeah, I think this was intended to be a 1-2 liner.
Steve
@wheelsofsteel
Mar 11 2017 20:48
If you don't mind, what exactly is \$1? I see it often but can't find a clear solution to it ever
most regex tutorials are really convoluted
itamargal
@itamargal
Mar 11 2017 20:49
It's JS syntax for a capture group reference.
It allows you to use to text which is matched by a regex pattern in your output.
In the example I just posted the capture group should match consonants that appear at the end of word, and the "$1" should refer to the specific consonant that's matched.
Steve
@wheelsofsteel
Mar 11 2017 20:51
OH
Right!
Exactly! THat's exactly what i wanted
itamargal
@itamargal
Mar 11 2017 20:51
I don't think the backslash is actually required (i.e. "\$1" works but so does "$1"), but I'm not sure. My JS is really weak.
Steve
@wheelsofsteel
Mar 11 2017 20:51
I wanted the equivalent of iterating over the array that string.match(regex) returns
but had no idea how to access it
so it woud be $1 (current value of the match array) + "err"?
itamargal
@itamargal
Mar 11 2017 20:53
Yes.
That's what this is doing: "\$1err"
Steve
@wheelsofsteel
Mar 11 2017 20:57
return string.replace(regex, "\$1err" ) doesn't seem to work, It's taking the $1 literally
itamargal
@itamargal
Mar 11 2017 20:57
Maybe you didn't include the parentheses in your regex?
i.e.: ([bcdfghjklmnpqrstvwxyz])
That's what denotes the capture group.
Steve
@wheelsofsteel
Mar 11 2017 21:02
I got it :), thanks a million!
itamargal
@itamargal
Mar 11 2017 21:02
Sure thing.
Steve
@wheelsofsteel
Mar 11 2017 21:29

I'm getting another weird error with this one: https://www.codewars.com/kata/remove-anchor-from-url/train/javascript. Code:

function removeUrlAnchor(url){
  return url.replace(url.substring(url.indexOf("#"), url.length), "");
}

But it says:

Expected 'www.codewars.com/katas/' but returned 'www.codewars.comkatas/'
I can add "/" to the end and solve that error but I'll just get new ones for the other tests.

Sorry, I meant:

function removeUrlAnchor(url){
  return url.replace(url.substr(url.indexOf("#")), "");
}

But, same difference

Steve
@wheelsofsteel
Mar 11 2017 21:43
Ah, I got it! Sorry :D