简体   繁体   English

使用箭头函数Javascript的简单成绩计算器

[英]simple grade calculator using arrow functions Javascript

I am trying to create a simple grade calculator that uses a function to determine a letter grade.我正在尝试创建一个简单的成绩计算器,它使用一个函数来确定字母等级。 I am using an arrow function to take a number and print out 'P' if its greater or equal to 70 and 'F' otherwise.我正在使用箭头函数取一个数字,如果它大于或等于 70,则打印出“P”,否则打印出“F”。

My script is not working properly.我的脚本工作不正常。 Anyone have good suggestions?有人有好的建议吗?

'use strict';

var grade = (a) => { if (a >= 70)
  return 'P';
  } else {
    return 'F';
  }
}

grade(80)

you are missing a curly brace after your if statement你在 if 语句之后缺少一个花括号

if(a >= 70)

should be应该

if(a >= 70) {

Well, you say "if it is greater or equal to 70" but your condition means "lower than or equal to 70".好吧,您说“如果它大于或等于 70”,但您的条件意味着“小于或等于 70”。

And you have a syntax error, missing a { after the condition.并且您有语法错误,在条件后缺少{

Finally, a shorter way of writing that arrow function would be:最后,编写该箭头函数的一种更简短的方法是:

var grade = a => (a >= 70) ? 'P' : 'F'

you are missing braces, check this fiddle http://jsfiddle.net/qmhwy5gu/ (see the result in console)您缺少大括号,请检查此小提琴http://jsfiddle.net/qmhwy5gu/ (请参阅控制台中的结果)

with this code it works使用此代码它可以工作

    var grade = (a) => { if (a >= 70){
    console.log('P');
    }else{
    console.log('F');
    }
}

grade(80)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM