Reputation: 195
First trial didn't work because string is immutable.
function LetterChanges(str) {
// want c->d & z->a; a,e,i,o,u capitalized
for(i=str.length-1;i>=0;i--){
if(str[i]=="c"){str[i]="d"}
if(str[i]=="z"){str[i]="a"}
if(str[i]=="a"||"e"||"i"||"o"||"u"){str[i]=str[i].toUpperCase()}
}
return str;
}
LetterChanges("I love you, Catz") // should return "AtAD ,UOy EvOl I"
Second trial: WHY DOES IT CAPITALIZE EVERYTHING? It should capitalize vowels only.
function LetterChanges(str) {
var changed = ""
// c->d & z->a; a,e,i,o,u capitalized
for(i=str.length-1;i>=0;i--){
if(str[i]=="c"){changed+="d"}
else if (str[i]=="C"){changed+="D"}
else if(str[i]=="z"){changed+="a"}
else if(str[i]=="Z"){changed+="A"}
else if(str[i]=="a"||"e"||"i"||"o"||"u"){changed+=str[i].toUpperCase()}
else {changed+=str[i]}
}
return changed;
}
LetterChanges("CapuoZ") // should return "AOUpAD" but instead it returns "AOUPAD", capitalizing non-vowels.
Upvotes: 0
Views: 88
Reputation: 3413
@Dalannar is right you can't directly change the values, here is the fiddle with your example working:
Your original post didn't unclude the upper case A and Z, here is the updated example: http://fiddle.jshell.net/rvxe8nq5/2/
function LetterChanges(str) {
// want c->d & z->a; a,e,i,o,u capitalized
var result = "";
for(i=str.length-1;i>=0;i--){
switch(str[i]) {
case "c":
result += "d";
break;
case "C":
result += "D";
break;
case "z":
result += "a";
break;
case "Z":
result += "A";
break;
case "a":
case "e":
case "i":
case "o":
case "u":
result += str[i].toUpperCase();
break;
default:
result += str[i];
}
}
return result;
}
alert(LetterChanges("I love you, Catz"));
Upvotes: 0
Reputation: 330
It is possible to do such operation in JS. The problem is as @Dalannar mansion, there isn't build-in option to set character at index. The following link contains replaceAt function witch implemented in JS:
How do I replace a character at a particular index in JavaScript?
(Copy the replaceAt function code)
Now you can modify character at index:
str = str.replaceAt(i, 'some string here');
And the last change is to make else-if condition
function LetterChanges(str) {
for(var i=str.length-1;i>=0;i--){
if (str[i] == 'c') {
str = str.replaceAt(i, 'd');
} else if (str[i] == 'z') {
str = str.replaceAt(i, 'a');
} else if(['a', 'e', 'i', 'o', 'u'].indexOf(str[i]) != -1) {
str=str.replaceAt(i, str[i].toUpperCase());
}
}
return str;
}
LetterChanges("I love you ccc"); // I lOvE yOU ddd
Upvotes: 0
Reputation: 1804
function LetterChanges(str) {
var newStr = str.replace("c", "d").replace("z", "a")
.replace("a", "A").replace("e", "E").replace("I", "i")
.replace("o", "O").replace("u", "U");
return newStr;
}
A better way to do it would be to store all the transformations in an array or list (like "c" -> "d") and then iterate over them
Upvotes: 0
Reputation: 381
String objects are immutable, which means you can't directly change their values. I'm sure there are better ways you can do it, but one simple way is to just create a second variable and keep adding to it as you go along.
Upvotes: 1