JavaScript LetterGrade function
JavaScript LetterGrade function
我想使用条件编写一个函数,该函数从作为参数传入的数字等级中确定字母等级。我用Python正确地编写了它,但我不确定JavaScript。
在JS中,它不断返回"你做了一个B","你做了一个(n)C",...,"你做了一个(n)F",无论我在函数中放置什么数字。
function letterGrade(grade) {
if (100>=grade && grade >= 90) {
alert('You made a(n) A.');}
if (80 <= grade && grade< 90) {
alert('You made a(n) B.');}
if (70<= grade && grade < 80) {
alert('You made a(n) C.');}
if (60<= grade && grade < 70); {
alert('You made a(n) D.');}
if (0<= grade && grade < 60) {
alert('You made a(n) F.');}
}
}
这个怎么样:
function letterGrade(grade) {
// singleDigitGrade will be an integer, 1-10
var singleDigitGrade = Math.floor(grade / 10);
if (singleDigitGrade === 9 || singleDigitGrade === 10) {
alert('You made a(n) A.');
} else if (singleDigitGrade === 8) {
alert('You made a(n) B.');
} else if ( singleDigitGrade === 7) {
alert('You made a(n) C.');
} else if (singleDigitGrade === 6) {
alert('You made a(n) D.');
} else {
alert('You made a(n) F.');
}
}
相关文章:
- 直接在函数声明上使用function.prototype.bind
- jQuery:.click(function(){(element),collapse('show',f
- 如果连接类型为none,则Javascript Function Only警报
- onLoad function phonegap android
- reducers在redux中得到Function not Object,what'it’他错了
- WIll window.addEventListener(“加载”,function(),false);等待浏览器自动填
- $(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进
- Javascript onunload function
- 在init function()中调用getElementById(“..”),而不是想用它做点什么的函数
- spyOn:应为间谍,但得到了Function
- (typeof variable === “function”) 和 jQuery.isFunction() 有什么区别
- this内部的值返回this.each(function(){})
- 如何在JavaScript中创建类似于something.function()的函数
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- 控制台中的“function floor(){[本机代码]}”是什么
- ASPX Jquery 1.11 $(Document).ready(function() {} was skipped
- Function.prototype.call和Function.protoype.all只应用一个参数
- 将angular javascript文件包装为“;(function(){..[js-code here]…})()&
- 从Function触发JQuery UI日期选择器,并将日期存储到变量中
- JavaScript LetterGrade function