Javascript 函数的if-else语句不起作用?
有人能告诉我为什么这不起作用吗?我尝试了很多改变结构的方法,但似乎不管我说什么,当if-else语句被应用时,它就停止工作了Javascript 函数的if-else语句不起作用?,javascript,conditional-statements,if-statement,Javascript,Conditional Statements,If Statement,有人能告诉我为什么这不起作用吗?我尝试了很多改变结构的方法,但似乎不管我说什么,当if-else语句被应用时,它就停止工作了 function wordSplit(){ var sentence = document.getElementById("two").value; var userWords=sentence.split(" "); while(t<userWords.length){ alert(u
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)
};
};
};
};
函数wordSplit(){
var语句=document.getElementById(“两个”).value;
var userWords=句子。拆分(“”);
虽然(t您没有关闭上一个警报中的引号:
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")
}
如果(y这个:
if(y这里是对代码的快速编辑,并对预期结果进行假设:
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');
}
}
}
函数wordSplit(){
var语句=document.getElementById(“两个”).value;
var userWords=句子。拆分(“”);
var t=0;
while(t
删除分号。语法是if(){}else{}
。if/else之间没有分号。另外,当你说“停止工作”时,我说你需要开始用控制台调试代码,这样你才能得到正确的错误消息。甚至在so上突出显示的语法也会显示(其中一个)问题…不要使用控制台-使用调试器,例如在Chrome中。
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");
}
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');
}
}
}