Javascript 函数返回 'undefined'

Javascript function returning 'undefined'

我写了一个函数,它将分数作为参数,应该 return 字母等级。 编写代码时需要遵循一些条件。即:return 'A' if 25 < score <= 30 return 'B' if 20 < score <= 25 等等。所以我想通过省略一大堆 if-else's 来做到这一点。由于我是 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;
}

第一个函数工作正常。 return是嵌套数组的索引。但是主函数getGrade恰好return'Undefined'。想不出比这更好的解决方案来减少一堆丑陋的 if-elses。

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

删除 Object.values 的外部 [] 数组。 Object.values array.

中已有 returns 个值

来自

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

var values = Object.values(gradeDict);

工作示例:

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));

备选方案

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));

Is there a better way to write this?

我愿意:

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

F 出现两次,因为映射到 F 的分数多于映射到其他年级的分数)

它可能有点神秘,但如果可能的分数发生变化,则更容易调整。

我喜欢之前提出的 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