强制 JavaScript 返回 false 以通过函数持久化

发布于 2024-12-06 18:47:13 字数 1032 浏览 0 评论 0原文

感谢您花时间查看这个问题。我已经尝试解决问题一两个小时但没有成功...

我有一个网页,它根据函数的响应设置 JavaScript 变量:

grade = getScore(questionAnswer, userAnswer, questionType);

(userAnswer 是用户对问题的回答并被检索来自文本区域)

这里是 getScore:

function getScore(questionAnswer, userAnswer, questionType) {

    switch(questionType) {
        case 'multiplechoice':
            return scoreMC(questionAnswer, userAnswer);
        break;

        case 'usertypesanswer':
            return scoreTA(questionAnswer, userAnswer);         
        break;

        default:
            return 0
    }   

}

scoreMC 和 ScoreTA 的函数已经过彻底测试并且运行良好。问题是,如果用户的答案格式不正确,scoreMC 或 ScoreTA 将返回 false。否则它返回值score 和msg。但是,当我根据 getScore 函数设置 Grade 变量的值时,我得到的不是“grade”的“false”值,而是“undefined”。 (当用户响应正确验证时,我们没有问题。)

设置“等级”后,我尝试检查它的任何部分是否未定义:

if(typeof(grade.score) !== undefined)

我不明白为什么,但即使我在 Firebug 控制台中看到“未定义” ,grade.score 通过了这项检查...

有人看到我做错了什么吗?非常感谢您的帮助。关于 JavaScript 我还有很多东西要学。

Thanks for taking time to review this question. I've been trying to fix a problem for one or two hours with no success...

I have a web page that sets a JavaScript variable based on the response from a function:

grade = getScore(questionAnswer, userAnswer, questionType);

(userAnswer is the user's answer to a question and is retrieved from a textarea)

Here is getScore:

function getScore(questionAnswer, userAnswer, questionType) {

    switch(questionType) {
        case 'multiplechoice':
            return scoreMC(questionAnswer, userAnswer);
        break;

        case 'usertypesanswer':
            return scoreTA(questionAnswer, userAnswer);         
        break;

        default:
            return 0
    }   

}

The functions for scoreMC and scoreTA have been tested thoroughly and work great. The issue is that if a user's answer is not formatted correctly, scoreMC or scoreTA will return false. Otherwise it returns the values score and msg. However, instead of getting a "false" value for "grade" when I set the value of the grade variable based on the getScore function, I get "undefined". (We have no problems when the user response validates properly.)

After setting "grade", I have tried to check if any part of it is undefined:

if(typeof(grade.score) !== undefined)

I do not understand why, but even when I see "undefined" in my Firebug console, grade.score passes this check...

Does anyone see what I am doing wrong? Thank you very much for your assistance. I have a lot to learn about JavaScript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼趣 2024-12-13 18:47:13
if(typeof(grade.score) !== undefined)

可以是

if(grade.score && grade.score !== false) // if I understand your question 

或者

if(typeof(grade.score) !== "undefined")

typeof 返回细绳

if(typeof(grade.score) !== undefined)

can be

if(grade.score && grade.score !== false) // if I understand your question 

or

if(typeof(grade.score) !== "undefined")

typeof returns a string

阳光下慵懒的猫 2024-12-13 18:47:13

如果没有使用 return 语句(或者没有值的空返回),JavaScript 将返回 undefined。

几乎可以肯定,您的评分函数之一(scoreMC、scoreTA,您应该将其代码包含在问题中)不会返回值,即

return;

或者只是到达函数代码块的末尾而没有遇到返回。

If no return statement is used (or an empty return with no value), JavaScript returns undefined.

It is almost certain that one of your score functions (scoreMC, scoreTA, whose code you should have included in the question) does not return a value i.e.

return;

Or just reaches the end of the function code block without encountering a return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文