有人可以告诉我为什么这不起作用?我已经尝试过改变结构了,但似乎无关紧要,当应用if else语句时,它就会停止工作。
function wordSplit(){
var sentence = document.getElementById("two").value;
var userWords=sentence.split(" ");
while(t<userWords.length){
alert(userWords[t]);
t++
};
x = 0;
for (var x = 0; x < userWords.length; x++){
y = 0;
for (var y = 0; y < vocab.length; y++){
if (y<vocab.length) {
alert("y is less than vocab")
};
else if (vocab[y] == userWords[x]){
alert("y is equal to x")
};
else if(y<vocab.length) {
alert("y is less than vocab 2")
};
else if (y == vocab.length){
alert(" y is equal to vocab length")
};
else if (y == 0)
{
alert("y is equal to zero)
};
};
};
};
答案 0 :(得分:0)
您没有关闭上次提醒中的报价:
alert("y is equal to zero)
正如评论所说的那样 - 删除if/else
之间的分号。
if (y<vocab.length) {
alert("y is less than vocab")
}
else if (vocab[y] == userWords[x]){
alert("y is equal to x")
}
else if(y<vocab.length) {
alert("y is less than vocab 2")
}
else if (y == vocab.length){
alert(" y is equal to vocab length")
}
else if (y == 0)
{
alert("y is equal to zero")
}
答案 1 :(得分:0)
这:
if (y<vocab.length) {
alert("y is less than vocab")
};
else if (vocab[y] == userWords[x]){
alert("y is equal to x")
};
else if(y<vocab.length) {
alert("y is less than vocab 2")
};
else if (y == vocab.length){
alert(" y is equal to vocab length")
};
else if (y == 0)
{
alert("y is equal to zero)
};
应该是这个(注意分号和结束“):
if (y<vocab.length) {
alert("y is less than vocab");
}
else if (vocab[y] == userWords[x]){
alert("y is equal to x");
}
else if(y<vocab.length) {
alert("y is less than vocab 2");
}
else if (y == vocab.length){
alert(" y is equal to vocab length");
}
else if (y == 0)
{
alert("y is equal to zero");
}
请注意,您的最终"
警告没有结束if else
。此外,分号(;
)不会出现在条件语句的末尾。
答案 2 :(得分:-1)
以下是对代码的快速编辑,其中包含对预期结果的假设:
function wordSplit() {
var sentence = document.getElementById("two").value;
var userWords=sentence.split(" ");
var t = 0;
while( t < userWords.length) {
console.log(userWords[t]);
t++;
}
for (var x = 0; x < userWords.length; x++) {
var y = vocab.indexOf(userWords[x]);
if (y == -1) {
console.log(userWords[x] + ' is not found in vabulary list');
} else {
console.log(userWords[x] + ' is found in vabulary list');
}
}
}