1

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 being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

I wrote out it out in JSBin and it worked fine (even te, but in CoderByte it didn't. I want to ask the community if what I wrote is correct and it's an issue on CoderByte, or if my code is wrong and the issue is with JSBin.

The code is as follows:

function LetterChanges(str) {
    var iLetters = str.split('');
    var newStr = [];

    for (var i = 0; i < str.length; i++) {
        if (/[a-y]/ig.test(iLetters[i])) {
            newStr[i] = String.fromCharCode(iLetters[i].charCodeAt(0) + 1);
            if (/[aeiou]/ig.test(newStr[i])) {
                newStr[i] = newStr[i].toUpperCase();
            }
        } else if (/[z]/ig.test(iLetters[i])) {
            newStr[i] = "A";
        } else if (/[^A-Z]/ig.test(iLetters[i])) {
            newStr[i] = iLetters[i];
        }
    }

    return newStr.join('');
}
Xotic750
  • 22,914
  • 8
  • 57
  • 79
DefionsCode
  • 483
  • 4
  • 8

3 Answers3

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():

function LetterChanges(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();
  });
}
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • +1 i love your answer its a brilliant answer. i must say because of your answer i had to brush off my regex skills(still needs to learn more on them). but have a look at mine solution. didn't get this part **c.toUpperCase() === 'Z' ? 'A' :** – Danish Feb 08 '16 at 08:43
  • @Danish It is the "z becomes a" part. The `toUpperCase` is applied to do this in a case-insensitive manner (z -> A, Z -> A). The "A" character is always uppercase, as the algorithm capitalizes all vowels in the end. – Fabrício Matté Feb 12 '16 at 18:54
  • thanks for answer. As algorithm is always capitalizing vowels in the end and program is already replace word with its next word using `String.fromCharCode(c.charCodeAt(0) + 1)` so, i wonder do we really need the "z becomes a" part. Cheers – Danish Feb 13 '16 at 18:48
  • @Danish The "z becomes a" part is requested in the question. Note that the charCode of "z" is 122, the next charCode being 123 which corresponds to the "{" character. Likewise, the next character after "Z" is "[" (using the charCode + 1 logic). That's why I explicitly coded the "z becomes a" part. – Fabrício Matté Feb 14 '16 at 23:31
  • @ FabrícioMatté thank you for your explanation. All clear now :) that's why i did +1 for you! – Danish Feb 15 '16 at 11:18
0

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.

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

    for (index = 0, length = str.length; index < length; index += 1) {
        code = str.charCodeAt(index);
        switch (code) {
            case 90:
                code = 65;
                break;
            case 122:
                code = 97
                break;
            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/:70
UIfO dbqjUbmjAf fwfsz wpxfm jO UIjt Ofx tUsjOh (b, f, j, p, v) bOE gjObmmz sfUvsO UIjt npEjgjfE tUsjOh. 
Xotic750
  • 22,914
  • 8
  • 57
  • 79
0

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.

function LetterChanges(str) { 
    return str.replace(/[a-z]/ig, function(Estr) {
        return String.fromCharCode(Estr.charCodeAt(0)+1);
    }).replace(/[aeiou]/ig, function(readyStr) {
        return readyStr.toUpperCase();
    })   
}
Danish
  • 1,467
  • 19
  • 28