4
06/11/2024 11:07 am
Topic starter
You are given an array of numbers. Write a JavaScript code/function that prints the elements at odd positions from the array, doubled and in reverse order. The input comes as array of number elements.
Examples:
Input:
[10, 15, 20, 25]
Output:
50 30
Input:
[3, 0, 10, 4, 7, 3]
Output:
6 8 0
The output is printed on the console on a single line, separated by space.
2 Answers
3
06/11/2024 11:08 am
My Js code:
function firstLastKElements(arrNum) { let odd = (arrNum.filter((num, i)=>i % 2 == 1));//filter the array to get the odd positions i //console.log(odd);//just checking let double = odd.map(x=>x * 2); //console.log(double);//just checking let reverse = (double.reverse()).join(" "); console.log(reverse); } firstLastKElements([10, 15, 20, 25]);
1
06/11/2024 11:09 am
My JS code:
function solve(arr) { let result = arr .filter((num, i)=>i % 2 == 1) .map(x=>x * 2) .reverse() .join(" "); console.log(result); //or in one row: //let result = arr.filter((num, i)=>i % 2 == 1).map(x=>x * 2).reverse().join(" "); } solve([3, 0, 10, 4, 7, 3]);