这段代码我写了什么错?
我试图让所有的常量都有旁边的操作,但没有任何工作。 这是这里的JavaScript代码。这段代码我写了什么错?
function opishConversion(text) {
var output = "";
for (var i = 0; i < text.length; i = i + 1) {
if ((text.charAt[i] !== "a") || (text.charAt[i] !== "A") || (text.charAt[i] !== "e") || (text.charAt[i] !== "E") || (text.charAt[i] !== "i") || (text.charAt[i] !== "I") || (text.charAt[i] !== "o") || (text.charAt[i] !== "O") || (text.charAt[i] !== "u") || (text.charAt[i] !== "U")) {
output += text.charAt[i] + "op";
} else {
output += text.charAt[i];
}
}
return output;
}
var text = prompt("Enter Text To Convert");
alert(opishConversion(text));
任何帮助,将不胜感激。
charAt
是String
原语的本地方法。它应该是charAt(i)
而不是charAt[i]
该算法也是错误的。 'charAt()'只是问题的一部分。 – 2014-11-14 19:26:05
我有信心修复阻止代码运行的语法错误将允许OP调试其余部分。 :) – Adam 2014-11-14 19:27:27
这不是一个语法错误,问题是要问代码有什么问题。 – 2014-11-14 19:30:57
string.charAt是一个函数,而不是索引对象。你需要使用parantheses而不是方括号。
这样:
而不是
text.charAt[i];
您还需要你的if语句改为
&&
的,而不是和
||
更正:
function opishConversion(text) {
var output = "";
for (var i = 0; i < text.length; i = i + 1) {
if ((text.charAt(i) !== "a") && (text.charAt(i) !== "A") && (text.charAt(i) !== "e") && (text.charAt(i) !== "E") && (text.charAt(i) !== "i") && (text.charAt(i) !== "I") && (text.charAt(i) !== "o") && (text.charAt(i) !== "O") && (text.charAt(i) !== "u") && (text.charAt(i) !== "U")) {
output += text.charAt(i) + "op";
} else {
output += text.charAt(i); //rather than text.charAt[i];
}
}
return output;
}
alert(opishConversion("aAbBcCdDeEfFgG"))
括号仍在'if'语句中。 – showdev 2014-11-14 19:17:07
好抓;逻辑也被关闭了。纠正! – 2014-11-14 19:20:27
chatAt()是一个函数,所以你不应该使用方括号
以上所有的答案将解决这个问题是你有。不过,我还建议你先简化逻辑,首先在for循环之前将文本改为小写。
function opishConversion(text) {
var output = '';
var text = text.toLowerCase();
for (var i = 0; i < text.length; i++) {
if ((text.charAt(i) !== "a") || (text.charAt(i) !== "e") || (text.charAt(i) !== "i") || (text.charAt(i) !== "o") || (text.charAt(i) !== "u")) {
output += text.charAt(i) + "op";
} else {
output += text.charAt(i);
}
}
return output;
}
var text = prompt("Enter Text To Convert");
alert(opishConversion(text));
你的意思是辅音,对不对?如在非元音字母中?常量!=辅音 – 2014-11-14 19:14:43
您需要提出一个问题来解决特定问题,并避免过于宽泛的问题,例如“我做错了什么?” – 2014-11-14 19:22:28
整个函数可以用'text.replace(/([^ aeiou])/ gi,“$ 1op”)' – 2014-11-14 19:23:54