If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.
If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.
Kata #10
DESCRIPTION:
Given a non-empty array of integers, return the result of multiplying the values together in order. Example:
[1, 2, 3, 4] => 1 * 2 * 3 * 4 = 24
Starting code:
function grow(x){
}
My attempt that worked:
function grow(x){
return x.reduce(function(product, n){
return product * n;
}, 1)
}
Rank #1 'Best practice':
const grow=x=> x.reduce((a,b) => a*b);
Rank #2 is also another one-liner:
const grow = (nums) => nums.reduce((product, num) => product * num, 1);
Unbelievably ranked higher is this one at #3:
const grow = x => {
let res = 1;
for (let i = 0; i < x.length; i++) {
res *= x[i];
}
return res;
};
Over this one at #4 which I reckon my solution falls under:
function grow(x){
return x.reduce((a, b)=> a * b,1);
}
I don't feel too bad because this is the first time I've deployed the .reduce method successfully. Actually feels pretty good to somehow cement the previous learning into place.
Worth including in mentions is this one at rank #5 which is another one-liner:
const grow=x=>eval(x.join("*"))
That's it for Kata #10, stay tuned for more katas to be solved!
Link to Kata #1: Square(n) Sum
Link to Kata #2: Convert a Number to a String
Link to Kata #3: DNA to RNA Conversion
Link to Kata #4: Remove First and Last Character
Link to Kata #5: MakeUpperCase
Link to Kata #6: Total amount of points
Link to Kata #7: A Needle in the Haystack