I recently pleted a JavaScript challenge asking to return a new array with all the values of the initial array doubled.
const numbers = [1, 2, 3];
function double() {
}
Except that I was to include some ES6 topics of de-structuring and rest/spread operators as well as recursion. Well, I pleted as best as I could to e to a solution. This was my solution:
const numbers = [1, 2, 3];
function double(arr){
const doubledNumbers = [];
for (var i = 0; i < arr.length; i ++){
const dubba = arr[i];
const bubba = dubba * 2;
doubledNumbers.push(bubba);
}
return doubledNumbers;
}
The other requirement was to not use any array helper method (map, reduce etc), so I did not use map()
, but instead a for
loop. However, I could not wrap my head around implementing de-structuring or rest/spread operators, concepts I thought I knew pretty well, never-mind recursion.
I recently pleted a JavaScript challenge asking to return a new array with all the values of the initial array doubled.
const numbers = [1, 2, 3];
function double() {
}
Except that I was to include some ES6 topics of de-structuring and rest/spread operators as well as recursion. Well, I pleted as best as I could to e to a solution. This was my solution:
const numbers = [1, 2, 3];
function double(arr){
const doubledNumbers = [];
for (var i = 0; i < arr.length; i ++){
const dubba = arr[i];
const bubba = dubba * 2;
doubledNumbers.push(bubba);
}
return doubledNumbers;
}
The other requirement was to not use any array helper method (map, reduce etc), so I did not use map()
, but instead a for
loop. However, I could not wrap my head around implementing de-structuring or rest/spread operators, concepts I thought I knew pretty well, never-mind recursion.
5 Answers
Reset to default 7Here's one possible implementation - destructure the parameter of double
, taking out the first number in the array, and use rest syntax to put the rest of the numbers into another array. Then, double
the rest of the array, and spread it into a new (returned) array, headed by the first number times 2:
const numbers = [1, 2, 3];
function double([firstNum, ...rest]) {
const restDoubled = rest.length ? double(rest) : [];
return [firstNum * 2, ...restDoubled];
}
console.log(double(numbers));
Here's a few alternatives -
const None =
Symbol("None")
const double = ([ x = None, ...more ]) =>
x === None
? []
: [ x * 2, ...double(more) ]
console.log(double([ 1, 2, 3, 4 ]))
// [ 2, 4, 6, 8 ]
Without destructuring -
const double = (nums = [], i = 0) =>
i >= nums.length
? []
: [ nums[i] * 2, ...double(nums, i + 1) ]
console.log(double([ 1, 2, 3, 4 ]))
// [ 2, 4, 6, 8 ]
Using higher-order functions (functions that accept or return other functions) -
const None =
Symbol("None")
const identity = x =>
x
const map = ([ x = None, ...more ], f = identity) =>
x === None
? []
: [ f(x), ...map(more, f) ]
const double = (nums = []) =>
map(nums, n => n * 2)
console.log(double([ 1, 2, 3, 4 ]))
// [ 2, 4, 6, 8 ]
Using continuation passing style -
const None =
Symbol("None")
const identity = x =>
x
const double = ([ x = None, ...more ], then = identity) =>
x === None
? then([])
: double(more, r => then([ x * 2, ...r ]))
double([ 1, 2, 3, 4 ], console.log)
// [ 2, 4, 6, 8 ]
Why to use recursion when we can do this using simple code as below
numbers = numbers.map(x => 2*x)
My solution is below -
const numbers = [1, 2, 3];
let finalResults = [];
function double(numbers) {
const [ number, ...rest ] = numbers;
if(number === undefined) {
return finalResults;
} else {
finalResults.push(number*2);
return double([...rest]);
}
}
A another way:
const numbers = [1, 2, 3];
const arr = [];
function double([x, ...some]) {
arr.push(x*2);
if(!some.length)
return arr;
return double(some);
}
double(1,2,3)
will return double(2,3)
which in turn will return double(3)
and finally double(3)
is going to return array [2,4,6]