Working on the following problem:
Create a function called alienLanguage
where the input will be a str
and the output should capitalize all letters except for the last letter of each word
alienLanguage("My name is John")
should return "My NAMe Is JOHn"
This is what I have coded:
function alienLanguage(str){
var words = str.toUpperCase().split(' ').map(function (a) {
return a.replace(a[a.length - 1], a[a.length - 1].toLowerCase())
});
return words.join(' ');
}
All of the example test cases work except for the following:
Expected: '\'THIs Is An EXAMPLe\'', instead got: '\'THIs Is An eXAMPLE\''
Why is the e in eXAMPLE
turning lowercase? Shouldn't everything automatically turn upperCase ?
edit: I just realized that there are 2 e
's and the first one is being replaced. How can I replace the last e
? and isn't the last character specified already?