首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不终止字符串常量与if else

不终止字符串常量与if else
EN

Stack Overflow用户
提问于 2013-08-09 16:37:41
回答 2查看 108关注 0票数 0

我是编码一个小测验,需要它显示一个特定的答案取决于分数。

当我刚刚显示分数时,我已经开始工作了,但是现在我已经添加了if added,它已经停止工作了,并且我得到了一个未终止的字符串常量。

代码语言:javascript
复制
$total_score = $first_question + $second_question + $third_question + $fourth_question + $fifth_question + $sixth_question + $seventh_question + $eighth_question + $ninth_question + $tenth_question + $eleventh_question + $twelfth_question + $thirteenth_question + $fourteenth_question + $fifthteenth_question + $sixteenth_question ;
});

$("#btn").click(function() {
   $('#reveal').html("<h3><strong>Total score</strong> " + $total_score +"</h3>"); 

if ($total_score >= 13) {
$('#answer').html("<h3><strong>13-16: Answer</strong></h3>"); 

} else if ($total_score >=9 && $total_score <= 12) {
$('#answer').html("<h3><strong>9-12: answer</strong></h3>"); 

} else if ($total_score >=5 && $total_score <= 8) {
$('#answer').html("<h3><strong>5-8: answer</strong></h3>"); 


 } else {
 $('#answer').html("<h3><strong> everything else</strong></h3>"); 
 }

});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-09 16:50:28

除了@Spork注意到的换行符外,您还需要在$total_score计算之后删除多余的“}”:

代码语言:javascript
复制
    $total_score = $first_question + $second_question + $third_question + $fourth_question + $fifth_question + $sixth_question + $seventh_question + $eighth_question + $ninth_question + $tenth_question + $eleventh_question + $twelfth_question + $thirteenth_question + $fourteenth_question + $fifthteenth_question + $sixteenth_question ;
    /* must remove the following:
    });
    */
    $("#btn").click(function() {
        $('#reveal').html("<h3><strong>Total score</strong> " + $total_score +"</h3>"); 

        if ($total_score >= 13) {
            $('#answer').html("<h3><strong>13-16: Answer </strong></h3>"); 

        } else if ($total_score >=9 && $total_score <= 12) {
            $('#answer').html("<h3><strong>9-12: answer </strong></h3>"); 

        } else if ($total_score >=5 && $total_score <= 8) {
            $('#answer').html("<h3><strong>5-8: answer </strong></h3>"); 
        } else {
            $('#answer').html("<h3><strong> everything else</strong></h3>"); 
        }

    });
票数 0
EN

Stack Overflow用户

发布于 2013-08-09 16:42:18

Javascript将行的结尾大致视为分号(;),因此不能像脚本中那样有多行字符串。把它们拿开,一切都会好的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18151558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档