I'm trying to analyze/reverse-engineer someone's Coderbyte code.The purpose of this challenge is to take a string parameter and to replace every letter of the string w/ the letter following it in the alphabet (a becomes b, z becomes a, etc). Then we are to capitalize every vowel in the new string (a, e, i, o, u). After finding someone's code, I was amazed at how he/she simplified it, but I had two specific questions:
1) what part of the code (see below) does the capitalizing??? I'm not seeing it so I must be missing it??? (watch it's probably going to be something completely obvious)
and
2) when would the else-statement come into play? the if-else is if (n>-1), so when would n <= -1? In the code, n is always going to be the index of a particular letter of the given string-parameter - it's index in oldAlph. That index would consist of 0 - 25. So I don't understand when it would be -1, or -2, etc...
Here is the code:
function LetterChanges(str) {
// code goes here
var n = 0;
var nstr = "";
var oldAlph = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var newAlph = ["b","c","d","E","f","g","h","I","j","k","l","m","n","O","p","q","r","s","t","U","v","w","x","y","z","A"];
for (i=0;i<str.length;i++) {
n = oldAlph.indexOf(str.substr(i,1));
if (n>-1) {
nstr = nstr + newAlph[n];
} else {
nstr = nstr + str.substr(i,1);
}
}
return nstr;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
LetterChanges(readline());
1: you are grabbing the capitalized version of the character and appending it to the new string here: nstr = nstr + newAlph[n];
2: it will be === -1 if the current character is not found within the oldAlph array, eg. a dot or a comma or an exclamation mark.. etc. In this case you don't need to perform any transformation on the character as it is not part of the alphabet.