没有任何内容被写入屏幕。
控制台错误显示如下:
ReferenceError:
ans
is not defined**
代码如下:
预期结果应该是将提示中的用户输入处理为小写并写入页面。
任何帮助都会被考虑。谢谢
function lowerCase(ans) {
var lowCase = ans.toLowerCase();
return lowerCase;
}
var questions = ['How may strings does a violin have?', 'How many sides does an octagon have?',
'How many NBA championships did Michael Jordan win with the Chicago Bulls?'
];
var answers = ['FOUR', 'EIGHT', 'SIX'];
var score = 0;
function quiz(counter) {
var guesses = 3;
while (guesses > 0) {
var ans = prompt(questions[counter]);
if (ans == answers[counter]) {
alert("Correct!");
return guesses;
} else {
guesses--;
alert("Incorrect, You have " + guesses + " guesses remaining");
}
}
return 0;
}
document.write(lowerCase(ans));
最佳答案
在函数中,不要使用 return lowerCase;
而是 return lowCase;
。您将再次返回该函数,而不是必须返回包含小写文本的变量。在全局范围内定义 ans,而不是在 while 循环内。您还必须调用测验功能。 document.write
应该位于测验函数内部而不是外部
function lowerCase(ans) {
var lowCase = ans.toLowerCase();
return lowCase;
}
var ans='';
var questions = ['How may strings does a violin have?', 'How many sides does an octagon have?',
'How many NBA championships did Michael Jordan win with the Chicago Bulls?'
];
var answers = ['FOUR', 'EIGHT', 'SIX'];
var score = 0;
function quiz(counter) {
var guesses = 3;
while (guesses > 0) {
ans = prompt(questions[counter]);
if (ans == answers[counter]) {
alert("Correct!");
return guesses;
} else {
guesses--;
alert("Incorrect, You have " + guesses + " guesses remaining");
}
}
document.write(lowerCase(ans));
}
quiz(2)
关于javascript - 将用户输入的答案转换为小写并写入屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55254735/