使用JavaScript中的递归查找最小的数字
我们需要编写一个JavaScript函数,该函数接受Numbers数组,并使用递归从中返回最小的数字。
假设以下是我们的数组-
const arr1 = [-2,-3,-4,-5,-6,-7,-8]; const arr2 = [-2, 5, 3, 0];
为此的代码将是-
const arr1 = [-2,-3,-4,-5,-6,-7,-8];
const arr2 = [-2, 5, 3, 0];
const min = arr => {
const helper = (a, ...res) => {
if (!res.length){
return a;
};
if (a < res[0]){
res[0] = a;
};
return helper(...res);
};
return helper(...arr);
}
console.log(min(arr1));
console.log(min(arr2));以下是控制台上的输出-
-8 -2
热门推荐
8 十月简短祝福语
10 婆婆生日祝福语简短的
11 生日牌祝福语英语简短
12 新年简短的祝福语爱情
13 浪漫的生日祝福语简短
14 简短56岁生日祝福语
15 学生开学季祝福语简短
16 送外卖祝福语大全简短
17 送女士祝福语长辈简短
18 写给男生简短的祝福语