首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数字至年级字母

数字至年级字母
EN

Stack Overflow用户
提问于 2019-07-02 19:39:08
回答 4查看 421关注 0票数 0

作为最近我的编码入门课程的一项任务,我们被要求创建一个函数,该函数将数字数组作为参数,并将它们输出到一系列的字母等级中。我被卡住了!

我尝试重新工作和重构我的代码,改变程序不同部分的位置,查看MDN.

代码语言:javascript
复制
    let grades = []

    function getLetterGrades(grades) {
      let grade = grades.map 
      if (grade < 60) {
       return "F";
      } else if (grade < 70) {
        return "D";
      } else if (grade < 80) {
        return "C";
      } else if (grade < 90) {
        return "B";
      } else if (grade < 100) {
        return "A";
      }
      console.log(grades);
    }

    getLetterGrades([95, 85, 71]);

结果将只输出我输入到函数调用中的数字。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-07-02 19:47:42

你的主要问题是:

代码语言:javascript
复制
let grade = grades.map 

您没有使用.map调用()方法,相反,grade正在结束对本机map函数的引用。而且,该函数不是一个数字,因此没有一个条件变为真,所以您可以继续通过if语句记录传入的数组。

相反,您必须调用.map()并提供它所需的参数(将为源数组中的每个项调用一个函数)。您的if语句应该是该函数的主体:

代码语言:javascript
复制
let grades = []

    function getLetterGrades(grades) {
      let letterGrades = grades.map(function(grade){
        if (grade < 60) {
         return "F";
        } else if (grade < 70) {
          return "D";
        } else if (grade < 80) {
          return "C";
        } else if (grade < 90) {
          return "B";
        } else if (grade < 100) {
          return "A";
        }      
      }); 

      console.log(letterGrades);
    }

    getLetterGrades([95, 85, 71]);

票数 1
EN

Stack Overflow用户

发布于 2019-07-02 19:45:43

你使用.map()是错误的。您所做的是将map方法与数字进行比较。你没有执行任何事情。

代码语言:javascript
复制
function getLetterGrades(grades) {
  return grades.map(function(grade) {
    if (grade < 60) {
      return "F";
    } else if (grade < 70) {
      return "D";
    } else if (grade < 80) {
      return "C";
    } else if (grade < 90) {
      return "B";
    } else if (grade < 100) {
      return "A";
    }
  });
}

var letters = getLetterGrades([95, 85, 71]);
console.log(letters)

票数 3
EN

Stack Overflow用户

发布于 2019-07-02 19:43:39

看看这个解决方案:

代码语言:javascript
复制
let grades = []

function getLetterGrades(grades) {
  // add an array (grade) that will hold the output
  let grade = []
  // iterate over grades with forEach()
  grades.forEach(item => {
    // item will be equal 95 on the first iteration
    // 85 on the second, and 71 on the third - these
    // values come from the passed 'grades' parameter
    if (item < 60) {
      grade.push("F");
    } else if (item < 70) {
      grade.push("D");
    } else if (item < 80) {
      grade.push("C");
    } else if (item < 90) {
      grade.push("B");
    } else if (item < 100) {
      grade.push("A");
    }
  })

  // console.log(grade) - NOT grades!
  console.log(grade);
}

getLetterGrades([95, 85, 71]);

问题不是你选择的方法-问题是你没有完成你的功能。下面是使用map()的另一种解决方案

代码语言:javascript
复制
let grades = []

function getLetterGrades(grades) {

  let grade = grades.map(item => {
    if (item < 60) {
      return "F";
    } else if (item < 70) {
      return "D";
    } else if (item < 80) {
      return "C";
    } else if (item < 90) {
      return "B";
    } else if (item < 100) {
      return "A";
    }
  })

  // console.log(grade) - NOT grades!
  console.log(grade);
}

getLetterGrades([95, 85, 71]);

在这种情况下,forEach()map()的主要区别是map()返回一个新数组(这就是为什么在函数体中使用return值),而forEach()不返回(我们必须手动创建数组grade,并将值推入这个“手工”数组)。

下面看一下,如果我们使用forEach()而没有手动创建数组,将会发生什么情况:

代码语言:javascript
复制
// THIS IS NOT A GOOD SOLUTION!
// IT GIVES YOU THE ANSWER IN THIS SMALL EXAMPLE
// (so you see that it's possible)
// BUT IN ANY LARGER CODE THIS IS THE
// 100% SURE SOURCE OF ERRORS.

let grades = []

function getLetterGrades(grades) {
  grades.forEach((item, index) => {
    if (item < 60) {
      grades[index] = "F";
    } else if (item < 70) {
      grades[index] = "D";
    } else if (item < 80) {
      grades[index] = "C";
    } else if (item < 90) {
      grades[index] = "B";
    } else if (item < 100) {
      grades[index] = "A";
    }
  })

  // console.log(grades) - NOT grade!
  console.log(grades);
}

getLetterGrades([95, 85, 71]);

(我使用了forEach()的第二个参数-这是索引)-这不是一个好的解决方案!,为什么?我们通过在grades中覆盖原始的getLetterGrades()数组来“摧毁”原始的数组--不要这样做

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

https://stackoverflow.com/questions/56859039

复制
相关文章

相似问题

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