【发布时间】:2020-03-25 13:20:51
【问题描述】:
const sortYears = function (arr){
arr.sort(function(a, b){return b - a})
}
const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
console.log(sortYears(years))
输出:未定义。我期待的输出:[ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ] 感谢一百万在这里的任何帮助
【问题讨论】:
-
您的函数没有返回任何内容,但您正在记录函数的返回值。要么返回一些东西,要么做
console.log(years)。 -
const sortYears = (arr) => arr.sort( (a, b)=> b - a );
标签: javascript sorting methods