You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// First we do a little meta-problem solving by setting up our alpha and newAlpha strings:
4
+
// Each character in alpha has the same index as the character coderbyte wants us to convert it to in newAlpha
5
+
// For instance, since we want all d's in our input string to be converted to e's, and then we want all vowels to be capitalized,
6
+
// we can "cheat" a by making alpha[3] equal to "d" and newAlpha[3] equal to "E".
7
+
varalpha='abcdefghijklmnopqrstuvwxyz';
8
+
varnewAlpha='bcdEfghIjklmnOpqrstUvwxyzA';
9
+
// Next, we declare a variable to hold our answer
10
+
varanswer='';
11
+
12
+
// After that, we loop through each character in our input string
13
+
for(vari=0;i<str.length;i++){
14
+
// First, we use the indexOf method to check if the current character in our string is contained in alpha.
15
+
// Note that if the string you pass into indexOf isn't found, it will return -1. Otherwise, it will return the index of the first matching character found.
16
+
// For instance, alpha.indexOf("c") returns 2, while alpha.indexOf("C") returns -1.
17
+
if(alpha.indexOf(str[i])!==-1){
18
+
// If we find the character in the alpha string, we declare a variable to hold the index of the character.
19
+
// Note that this is an unnessary step that I do for the purposes of clarity. See the 2nd function for a faster implementation.
20
+
varindex=alpha.indexOf(str[i]);
21
+
// Since we set up the characters in alpha to have the same index as the one we want to convert it to in newAlpha,
22
+
// all we have to do is access newAlpha at index to add the converted character to our answer variable.
23
+
answer+=newAlpha[index];
24
+
// If str[i] doesn't appear in alpha...
25
+
}else{
26
+
// ...we add it to our answer string, leaving any characters we don't want to change untouched and in the same index in our answer variable as they were in our input string.
0 commit comments