I'm doing a beginner exercise, create a rövarspråket translator.
Write a function translate() that will translate a text into “rövarspråket”. That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".
The solutions I was coming up with were bad, so I found one here and am trying to understand it.
var translate = function(text) {
var string = text.toLowerCase();
var vowels = ["a", "e", "i", "o", "u", " "];
var y = "";
for (i = 0; i < string.length; i++) {
var current = string.charAt(i);
if (vowels.indexOf(current) != -1) {
y = (y + (current));
} else {
y = (y + (current + "o" + current));
}
}
return y;
}
console.log(translate("this is fun"));
-
Why does
if (vowels.indexOf(current) != -1)
need-1
specifically? I tried-2
,-1000
,1000
, but they all break the function. -
If I change
y = "";
toy = "XYZ"
,translate("this is fun")
returns "XYZtothohisos isos fofunon". Why is it only before the first consonant and not all of them?
Aucun commentaire:
Enregistrer un commentaire