Javascript 经典元音练习。我能';我不明白为什么我的for循环是';行不通
练习问题-编写一个函数translate(),将文本转换为“rövarspråket”。也就是说,将每个辅音加倍,并在两者之间加上一个“o”。例如,translate(“this is fun”)应该返回字符串“tothohisos isos fofunon” 我不明白为什么我的代码不起作用Javascript 经典元音练习。我能';我不明白为什么我的for循环是';行不通,javascript,html,Javascript,Html,练习问题-编写一个函数translate(),将文本转换为“rövarspråket”。也就是说,将每个辅音加倍,并在两者之间加上一个“o”。例如,translate(“this is fun”)应该返回字符串“tothohisos isos fofunon” 我不明白为什么我的代码不起作用 var word = prompt("Enter a word"); var vowels = ["a", "e", "i" ,"o", "u"]; var output; for (var i=0;
var word = prompt("Enter a word");
var vowels = ["a", "e", "i" ,"o", "u"];
var output;
for (var i=0; i < word.length; i++) {
if (word.charAt(i) != "a" || "e" || "i" || "o" || "u" ) {
output = word.charAt(i) + "o" + word.charAt(i);
} else {
output = word.charAt(i);
}
document.getElementById("paragraph").innerHTML = output;
}
var word=prompt(“输入单词”);
可变元音=[“a”、“e”、“i”、“o”、“u”];
var输出;
for(变量i=0;i
试试这个:
var word = prompt("Enter a word");
var vowels = ["a", "e", "i" ,"o", "u"];
var output;
for (var i=0; i < word.length; i++) {
if (vowels.indexOf(word.charAt(i))==-1 ) {
output += word.charAt(i) + "o" + word.charAt(i);
}
else{
output += word.charAt(i);
}
document.getElementById("paragraph").innerHTML = output;
}
var word=prompt(“输入单词”);
可变元音=[“a”、“e”、“i”、“o”、“u”];
var输出;
for(变量i=0;i
请注意,我将单词.charAt(i)替换为元音.indexOf
通过使用indexOf,您可以通过返回值确定数组中是否存在某些内容。indexOf返回-1如果元素不存在或数组中元素的索引不存在,请尝试以下操作:
var word = prompt("Enter a word");
var vowels = ["a", "e", "i" ,"o", "u"];
var output;
for (var i=0; i < word.length; i++) {
if (vowels.indexOf(word.charAt(i))==-1 ) {
output += word.charAt(i) + "o" + word.charAt(i);
}
else{
output += word.charAt(i);
}
document.getElementById("paragraph").innerHTML = output;
}
var word=prompt(“输入单词”);
可变元音=[“a”、“e”、“i”、“o”、“u”];
var输出;
for(变量i=0;i
请注意,我将单词.charAt(i)替换为元音.indexOf
通过使用indexOf,您可以通过返回值确定数组中是否存在某些内容。如果元素不存在,indexOf将返回-1,或者数组中元素的索引将返回此代码
if (word.charAt(i) != "a" || "e" || "i" || "o" || "u" ) {...}
意味着
如果[(word.charAt(i)!=“a”)
或“e”或“a”
…]
当强制转换为布尔值时,“e”的计算结果为true
正确的代码是
if (word.charAt(i) != "a") && (word.charAt(i) != "e") && (word.charAt(i) != "i") && (word.charAt(i) != "o") && (word.charAt(i) != "u") ) {...}
此外,将output=
命令更改为output+=
此代码
if (word.charAt(i) != "a" || "e" || "i" || "o" || "u" ) {...}
意味着
如果[(word.charAt(i)!=“a”)
或“e”或“a”
…]
当强制转换为布尔值时,“e”的计算结果为true
正确的代码是
if (word.charAt(i) != "a") && (word.charAt(i) != "e") && (word.charAt(i) != "i") && (word.charAt(i) != "o") && (word.charAt(i) != "u") ) {...}
此外,将output=
命令更改为output+=
这可以通过使用regex在一行中实现:
yourString.replace(/([bcdfghjklmnpqrstvwxz])/g, '$1o$1'); // Add the accepted characters here
在您的示例中:
"this is fun".replace(/([bcdfghjklmnpqrstvwxz])/g, '$1o$1')
输出
"tothohisos isos fofunon"
这可以通过使用正则表达式在一行中实现:
yourString.replace(/([bcdfghjklmnpqrstvwxz])/g, '$1o$1'); // Add the accepted characters here
在您的示例中:
"this is fun".replace(/([bcdfghjklmnpqrstvwxz])/g, '$1o$1')
输出
"tothohisos isos fofunon"
if(word.charAt(i)!=“a”| |“e”| |“i”|“o”| |“u”){
不正确-无法一次将一个变量与多个值进行比较。为什么不使用元音数组?这不是进行多重比较的方式。嗯,好的,我没有意识到。我将使用数组重试。谢谢。var output=';
输出=output+…;
您的输出变量将每一次重置一次循环增加。if(word.charAt(i)!=“a”| | |“e”| |“i”| |“o”| |“u”){
不正确-无法一次将一个变量与多个值进行比较。为什么不使用元音数组?这不是进行多重比较的方式。嗯,好的,我没有意识到。我将使用数组重试。谢谢。var output=';
输出=output+…;
您的输出变量将每一次重置一次循环增加。output=output+…;
output=output+…;So:if(word.charAt(i)!=“a”|“e”与:if(word.charAt(i)!=“a”| true?是的,基本上是正确的。因此if条件的计算结果将始终与您编写的相同。因此:if(word.charAt(i)!=“a”|“e”与:if(word.charAt(i)!)相同“a”| |真的?是的,基本上是对的。因此if条件的计算结果将始终与您编写的一样真实。您的方法非常好,谢谢。我很固执,试图找出OR运算符。您的方法非常好,谢谢。我很固执,试图找出OR运算符。