Skip to content Skip to sidebar Skip to footer

Changing Letters Algorithm, Works In Jsbin But Not In Coderbyte, Seeking Clarification

I'm going through the CoderByte exercises and I came across the following problem: >Using the JavaScript language, have the function LetterChanges(str) take the str parameter b

Solution 1:

Seems like a bug on their back-end JS runner indeed. As you've stated, your code runs fine and should be accepted. Worth reporting it to their support imo.

Here's an alternative solution specifying a function as second parameter to .replace():

functionLetterChanges(str) {
  return str.replace(/[a-z]/ig, function(c) {
    return c.toUpperCase() === 'Z' ? 'A' : String.fromCharCode(c.charCodeAt(0) + 1);
  }).replace(/[aeiou]/g, function(c) {
    return c.toUpperCase();
  });
}

Solution 2:

Your code worked just fine for me on jsfiddle when compared against the following alternative

On CoderByte, your code failed but the following worked. Seems to be a problem on their site.

functionletterChanges(str) {
    var newString = "",
        code,
        length,
        index;

    for (index = 0, length = str.length; index < length; index += 1) {
        code = str.charCodeAt(index);
        switch (code) {
            case90:
                code = 65;
                break;
            case122:
                code = 97break;
            default:
                if ((code >= 65 && code < 90) || (code >= 97 && code < 122)) {
                    code += 1;
                }
        }

        newString += String.fromCharCode(code);
    }

    return newString.replace(/[aeiou]/g, function (character) {
        return character.toUpperCase();
    });
}

console.log(LetterChanges("Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string."));
console.log(letterChanges("Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string."));

Output

UIfO dbqjUbmjAf fwfsz wpxfm jO UIjt Ofx tUsjOh (b, f, j, p, v) bOE gjObmmz sfUvsO UIjt npEjgjfE tUsjOh. fiddle.jshell.net/:70UIfO dbqjUbmjAf fwfsz wpxfm jO UIjt Ofx tUsjOh (b, f, j, p, v) bOE gjObmmz sfUvsO UIjt npEjgjfE tUsjOh. 

Solution 3:

Just another solution from the answer of @Fabrício Matté and a bit explanation is that using regex getting first alphabets from a to z using /[a-z]/ and replacing them by adding one to ASCII of the each string using String.fromCharCode(Estr.charCodeAt(0)+1) and the rest is matter of finding vowel's using again regex [aeiou] and returning capitalized string of it.

functionLetterChanges(str) { 
    return str.replace(/[a-z]/ig, function(Estr) {
        returnString.fromCharCode(Estr.charCodeAt(0)+1);
    }).replace(/[aeiou]/ig, function(readyStr) {
        return readyStr.toUpperCase();
    })   
}

Post a Comment for "Changing Letters Algorithm, Works In Jsbin But Not In Coderbyte, Seeking Clarification"