作为最近我的编码入门课程的一项任务,我们被要求创建一个函数,该函数将数字数组作为参数,并将它们输出到一系列的字母等级中。我被卡住了!
我尝试重新工作和重构我的代码,改变程序不同部分的位置,查看MDN.
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]);
结果将只输出我输入到函数调用中的数字。
发布于 2019-07-02 19:47:42
你的主要问题是:
let grade = grades.map 您没有使用.map调用()方法,相反,grade正在结束对本机map函数的引用。而且,该函数不是一个数字,因此没有一个条件变为真,所以您可以继续通过if语句记录传入的数组。
相反,您必须调用.map()并提供它所需的参数(将为源数组中的每个项调用一个函数)。您的if语句应该是该函数的主体:
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]);
发布于 2019-07-02 19:45:43
你使用.map()是错误的。您所做的是将map方法与数字进行比较。你没有执行任何事情。
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)
发布于 2019-07-02 19:43:39
看看这个解决方案:
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()的另一种解决方案
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()而没有手动创建数组,将会发生什么情况:
// 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()数组来“摧毁”原始的数组--不要这样做!
https://stackoverflow.com/questions/56859039
复制相似问题