简体   繁体   English

Javascript function 返回“未定义”

[英]Javascript function returning 'undefined'

I've written aa function which takes score as parameter and should return the letter grade.我写了一个 function ,它以分数作为参数,应该返回字母等级。 There are some conditions to be followed while writing the code.编写代码时需要遵循一些条件。 ie: return 'A' if 25 < score <= 30 return 'B' if 20 < score <= 25 and so on.即: return 'A' if 25 < score <= 30 return 'B' if 20 < score <= 25等等。 So I wanted to do this by omitting a whole lot of if-else's .所以我想通过省略很多if-else's来做到这一点。 As I'm new to javascript this is all I could come up with:因为我是 javascript 的新手,所以我能想到的就是:

// This function takes Nested arrays and a single number, 
// which checks its availability in 
// the inside array and then return the index of the array
function my_index(arr, score) {
    for (const [index, elem] of arr.entries()) {
        if (elem.includes(score)) {
            return index;
        }
        
    }
}

// function to get letter grade
function getGrade(score) {
    let grade;
    var gradeDict = {
        'A': [26, 27, 28, 29, 30],
        'B': [21, 22, 23, 24, 25],
        'C': [16, 17, 18, 19, 20],
        'D': [11, 12, 13, 14, 15],
        'E': [6, 7, 8, 9, 10],
        'F': [0, 1, 2, 3, 4, 5] 
    }
    var keys = Object.keys(gradeDict);
    var values = [Object.values(gradeDict)]
    grade = keys[my_index(values, score)]
        
    return grade;
}

The first function works fine.第一个 function 工作正常。 It returns the index of nested array.它返回嵌套数组的索引。 But the main function getGrade happens to return 'Undefined' .但是主要的 function getGrade恰好返回'Undefined' Can't think of a better solution than this to reduce a bunch of ugly if-elses.想不出比这更好的解决方案来减少一堆丑陋的 if-else。

var question = {
    '1st': 'Can anybody help me get this done?',
    '2nd': 'Is there any better way to do this?'
}

Is there a better way to write this?有没有更好的方法来写这个?

I'd do:我会做:

function getLetterGrade(score) {
  return ['F', 'F', 'E', 'D', 'C', 'B', 'A'][Math.ceil(score / 5)];
}

( F occurs twice because more scores map to F than to other grades) F出现两次,因为 map 到 F 的分数比其他等级的分数多)

It may be a bit cryptic, but is easier to tune should the possible score ever change.它可能有点神秘,但如果可能的分数发生变化,它更容易调整。

Remove the outer [] array of the Object.values .移除Object.values的外部[]数组。 Object.values already returns values in array . Object.values已经返回array中的值。

from

var values = [Object.values(gradeDict)];

to

var values = Object.values(gradeDict);

working example:工作示例:

 function my_index(arr, score) { for (const [index, elem] of arr.entries()) { if (elem.includes(score)) { return index; } } } function getGrade(score) { let grade; var gradeDict = { A: [26, 27, 28, 29, 30], B: [21, 22, 23, 24, 25], C: [16, 17, 18, 19, 20], D: [11, 12, 13, 14, 15], E: [6, 7, 8, 9, 10], F: [0, 1, 2, 3, 4, 5], }; var keys = Object.keys(gradeDict); var values = Object.values(gradeDict); grade = keys[my_index(values, score)]; return grade; } console.log(getGrade(5)); console.log(getGrade(25));

Alternate solution替代解决方案

 function getGrade(score) { let grade; var gradeDict = { A: [26, 27, 28, 29, 30], B: [21, 22, 23, 24, 25], C: [16, 17, 18, 19, 20], D: [11, 12, 13, 14, 15], E: [6, 7, 8, 9, 10], F: [0, 1, 2, 3, 4, 5], }; for (let key in gradeDict) { if (gradeDict[key].includes(score)) return key; } return "Not found"; } console.log(getGrade(5)); console.log(getGrade(25));

I like the ceil solution proposed earlier, but here is another general solution in case it's helpful:我喜欢前面提出的ceil解决方案,但如果有帮助,这里有另一个通用解决方案:

function grade(score) {
  if (score < 0 || score > 30) throw RangeError(`Score ${score} out of range`);

  for (let ii = 5; ii >= 0; ii--) {
    if (score > 5*ii) return String.fromCharCode(70 - ii);
  }

  return 'F';
}

console.log(0, '=>', grade(0))   // F
console.log(4, '=>', grade(4))   // F
console.log(6, '=>', grade(6))   // E
console.log(10, '=>', grade(10)) // E
console.log(27, '=>', grade(27)) // A
console.log(30, '=>', grade(30)) // A

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

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