试着写一些小程序进行学习,第一次使用自调用函数。无法理解返回结果值的原因(正如我在Chrome Sources调试器中看到的那样),但未按预期在控制台中打印出来。
提前感谢您的解释。
function secondLowestAndHighest(numbers) {
//an empty array for the result
let result = new Array;
//sorting out the array by ascending order with built-in method .sort()
let lowToHigh = numbers.sort(function(a, b) {
return a - b;
});
//reverse the array of numbers
let highToLow = new Array;
(function() {
//self-invoked function, to call out automatically
for (let i = lowToHigh.length - 1; i >= 0; i--) {
highToLow.push(lowToHigh[i]);
}
return highToLow;
})();
result.push(lowToHigh[1], highToLow[1]);
return result;
}
//secondLowestAndHighest([13, 105, 75, 1, 344, 6, 5]);
console.log(secondLowestAndHighest([13, 105, 75, 1, 344, 6, 5]))