JavaScript显示分数&基于不同条件的消息

JavaScript displaying scores & messages based on different conditions

本文关键字:于不同 条件 消息 amp 显示 JavaScript      更新时间:2023-09-26

JavaScript嵌套if condition。

JS代码向用户询问了2个问题,每个问题都有4个选项可供选择作为答案,每个答案都有一个特定的值,如果用户的分数是6分或更多,那么每个选择的答案都会得到附加评论,得到值4的答案将得到附加评论。我试图实现它,但我没有成功。

举个例子:

示例:

问题01:你喜欢巧克力吗?答:是的,很多。

问题02:你喜欢巧克力蛋糕还是胡萝卜蛋糕?答案:两者都有。

第一个答案的值是4,第二个是2,所以总共得了6分。

var health = 'Very Healthy';
var average = 'Neither Healthy nor unhealthy';
var unhealthy = 'Unhealthy';

对正确的解决方案有什么建议吗?

我在下面的fiddle链接中保存了我的最新想法=>http://jsfiddle.net/Hf68f/

在最后两个函数(getAdditionalComment1getAdditionalComment2)中,出现语法错误。您使用的是()而不是{}来封装函数。它应该看起来像

function getAdditionalComment1(score, scoreChoco)
{ // Changed here
    if(score >=6){
        if (scoreChoco == 4)
        return additionalCommentYesAlot;
        else
        return "";
    }
} // And here
function getAdditionalComment2(score, scoreCake)
{ // And here
    if(score >=6){
        if (scoreCake == 4)
        return addionalCommentChocolate;
        else
        return "";
    }
} // And here

一旦语法错误得到解决,代码在我的测试中运行良好。

我已经清理了您的代码,使添加新问题变得容易,避免了几乎重复的函数,并使用此答案中的代码来选择消息:

var numericalValues = {
    Alot: 4,
    NotMuch: 2,
    NoSometimes: 3,
    Hate: 0,
    Chocolate: 4,
    Carrot: 0,
    Both: 2,
    None: 0
};
function getScore(name) {
    var form = document.forms["form"],
        els = form.elements[name];
    for(var i=0; i<els.length; i++)
        if(els[i].checked)
            return numericalValues[els[i].value];
}
var names = ['cake', 'choco'];
function getTotal() {
    var scores = [], totalScore = 0;
    for(var i=0; i<names.length; ++i)
        totalScore += scores[names[i]] = getScore(names[i]);
    document.getElementById('result').innerHTML = 
        getComment(totalScore)
        + '. '+
        getAdditionalComment(scores);
}
var comments = [
    [0, 'Very Healthy'],                  /* For 0,1,2        */
    [3, 'Neither Healthy nor unhealthy'], /* For 3,4,5,6      */
    [7, 'Unhealthy']                      /* For 7...Infinity */
],
additionalComments = {
    choco: [
        [4, 'you eat too much chocolate']/*,
        [5, void 0] // Not needed since 4 is maximum */
    ],
    cake: [
        [4, 'you have to start a diet']/*,
        [5, void 0] // Not needed since 4 is maximum */
    ]
};
function getValueInRange(arr, n, from, to) {
    return (function main(from, to){
        if(from>=to) return void 0;
        var mid = Math.floor((from+to)/2);
        if(arr[mid][0] > n) return main(from, mid);
        if(arr[mid][0] < n && mid > from) return main(mid, to);
        return arr[mid][1];
    })(from===void 0 ? 0 : from, to===void 0 ? arr.length : to);
}
function getComment(score) {
    return getValueInRange(comments, score);
}
function getAdditionalComment(scores) {
    var arr = [];
    for(var i=0, l=names.length; i<l; ++i) {
        var txt = getValueInRange(
            additionalComments[names[i]],
            scores[names[i]]
        );
        if(txt) arr.push(txt);
    }
    return arr.join(', ');
}
document.getElementById('calculate').onclick=getTotal;

演示