如何将奇数索引值相乘JavaScript
我们需要编写一个函数,该函数将Number文字数组作为一个唯一的参数。位于偶数索引的数字应原样返回。但是,应返回位于奇数索引处的数字乘以其对应的索引。
例如-
If the input is: [5, 10, 15, 20, 25, 30, 50, 100] Then the function should return: [5, 10, 15, 60, 25, 150, 50, 700]
我们将使用Array.prototype.reduce()方法构造所需的数组,该函数的代码为-
示例
const arr = [5, 10, 15, 20, 25, 30, 50, 100];
const multiplyOdd = (arr) => {
return arr.reduce((acc, val, ind) => {
if(ind % 2 === 1){
val *= ind;
};
return acc.concat(val);
}, []);
};
console.log(multiplyOdd(arr));输出结果
控制台中的输出将为-
[ 5, 10, 15, 60, 25, 150, 50, 700 ]
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练