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

JavaScript显示分数&;基于不同条件的消息,javascript,Javascript,JavaScript嵌套条件 JS代码向用户询问了2个问题,每个问题有4个选项可供选择作为答案,每个答案都有一个特定的值,如果用户的分数为6分或更多,则每个选择的答案(值为4)都会得到额外的注释,我试图实现它,但我没有管理它 举个例子: 例如: 问题01:你喜欢巧克力吗?答:是的,很多 问题02:你喜欢巧克力蛋糕还是胡萝卜蛋糕?答:两者都有 第一个答案的值为4,第二个答案的值为2,所以总共得6分 var health = 'Very Healthy'; var average = 'Neithe

JavaScript嵌套条件

JS代码向用户询问了2个问题,每个问题有4个选项可供选择作为答案,每个答案都有一个特定的值,如果用户的分数为6分或更多,则每个选择的答案(值为4)都会得到额外的注释,我试图实现它,但我没有管理它

举个例子:

例如:

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

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

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

var health = 'Very Healthy';
var average = 'Neither Healthy nor unhealthy';
var unhealthy = 'Unhealthy';
对正确的解决方案有什么建议吗


我在下面的fiddle链接中保存了我的最新想法=>

在您的最后两个函数(
getAdditionalComment1
getAdditionalComment2
)中,您有一个语法错误。您使用的是
()
而不是
{}
来封装函数。应该是这样的

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

语法错误解决后,代码在我的测试中可以正常运行。

在您的最后两个函数(
getAdditionalComment1
getAdditionalComment2
)中,出现语法错误。您使用的是
()
而不是
{}
来封装函数。应该是这样的

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

语法错误解决后,代码在我的测试中可以正常运行。

在您的最后两个函数(
getAdditionalComment1
getAdditionalComment2
)中,出现语法错误。您使用的是
()
而不是
{}
来封装函数。应该是这样的

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

语法错误解决后,代码在我的测试中可以正常运行。

在您的最后两个函数(
getAdditionalComment1
getAdditionalComment2
)中,出现语法错误。您使用的是
()
而不是
{}
来封装函数。应该是这样的

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;
var数值={
很多:4,
不多:2,
有时:3,
仇恨:0,
巧克力:4,
胡萝卜:0,
二,,
无:0
};
函数getScore(名称){
var form=document.forms[“form”],
els=形式元素[名称];
对于(var i=0;i n)回流干管(从,中间);
如果(arr[mid][0]from)返回主管道(mid,to);
返回arr[mid][1];
})(从===void 0?0:from,到===void 0?arr.length:to);
}
函数getComment(score){
返回getValueInRange(注释、分数);
}
函数getAdditionalComment(分数){
var-arr=[];

对于(var i=0,l=names.length;i我已经清理了您的代码,使添加新问题变得容易,避免了几乎重复的函数,并使用代码从中选择消息:

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;
var数值={
很多:4,
不多:2,
有时:3,
仇恨:0,
巧克力:4,
胡萝卜:0,
二,,
无:0
};
函数getScore(名称){
var form=document.forms[“form”],
els=形式元素[名称];
对于(var i=0;i n)回流干管(从,中间);
如果(arr[mid][0]from)返回主管道(mid,to);
返回arr[mid][1];
})(从===void 0?0:from,到===void 0?arr.length:to);
}
函数getComment(score){
返回getValueInRange(注释、分数);
}
函数getAdditionalComment(分数){
var-arr=[];

对于(var i=0,l=names.length;i我已经清理了您的代码,使添加新问题变得容易,避免了几乎重复的函数,并使用代码从中选择消息:

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;
var数值={
很多:4,
不多:2,
有时:3,
仇恨:0,
巧克力:4,
胡萝卜:0,
二,,
无:0
};
函数getScore(名称){
var form=document.forms[“form”],
els=形式元素[名称];
对于(var i=0;i n)回流干管(从,中间);
如果(arr[mid][0]from)返回主管道(mid,to);
返回arr[mid][1];
})(从===void 0?0:from,到===void 0?arr.length:to);
}
函数getComment(score){
返回getValueInRange(注释、分数);
}
函数getAdditionalComment(分数){
var-arr=[];

对于(var i=0,l=names.length;i我已经清理了您的代码,使添加新问题变得容易,避免了几乎重复的函数,并使用代码从中选择消息:

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;
var数值={
很多:4,
不多:2,
有时:3,
仇恨:0,
巧克力:4,
胡萝卜:0,
二,,
无:0
};
函数getScore(名称){
var form=document.forms[“form”],
els=形式元素[名称];
对于(var i=0;i n)回流干管(从,中间);
如果(arr[mid][0]from)返回主管道(mid,to);
返回arr[mid][1];
})(从===void 0?0:from,到===void 0?arr.length:to);
}
函数getComment(score){
返回getValueInRange(注释、分数);
}
函数getAdditionalComment(分数){
var-arr=[];

对于(var i=0,l=names.length;iIn GetAdditionalComment2中有一个未定义的变量,名为scoreCake。@丹·艾弗森,是的,我无意中犯了一个错误,好的,我已经纠正了,但代码仍然不起作用。GetAdditionalComment2中有一个未定义的变量,名为scoreCake。@丹·艾弗森,是的,我无意中犯了一个错误,好的,我没有他纠正了错误,但代码仍然不起作用。在GetAdditionalComment2中有一个未定义的变量称为scoreCake。@丹·艾弗森,是的,我无意中犯了一个错误,好的,我已经纠正了错误,但代码仍然不起作用。在GetAdditionalComment2中有一个未定义的变量称为scoreCake。@丹·艾弗森,是的,我偶然犯了一个错误t、 好的,我已经做了更正,但代码仍然不起作用。谢谢,我犯了很愚蠢的错误,我创建了一个新的小提琴:,它没有按照我希望的方式工作,