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 #29
DESCRIPTION:
Create a function with two arguments that will return an array of the first (n) multiples of (x).
Assume both the given number and the number of times to count will be positive numbers greater than 0.
Return the results as an array (or list in Python, Haskell or Elixir).
Examples:
countBy(1,10) === [1,2,3,4,5,6,7,8,9,10]
countBy(2,5) === [2,4,6,8,10]
Starting code:
function countBy(x, n) {
let z = [];
return z;
}
My attempt:
function countBy(x, n) {
let z = [];
for (i=1;i<=n;i++){
z.push(x*i)
}
return z;
}
Rank #1 in 'Best Practice':
function countBy(x, n) {
var z = [];
for (i = 1; i <= n; i++) {
z.push(x * i);
}
return z;
}
Rank #2 with the one-liner and using Array.from():
const countBy = (x, n) => Array.from({length: n}, (v, k) => (k + 1) * x)
Rank #3 was not very different with rank #1:
function countBy(x, n) {
var z = []
for (var i = 1; i <= n; i++) {
z.push(x* i);
}
return z
}
Rank #4 with another one-liner but using .map() by user 0lexa:
const countBy = (x, n) => [...Array(n)].map((_, idx) => ++idx * x);
That's it for Kata #29, 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
Link to Kata #8: Sum of positive
Link to Kata #9: Basic Mathematical Operations
Link to Kata #10: Beginner - Reduce but Grow
Link to Kata #11: Square Every Digit
Link to Kata #12: Friend or Foe?
Link to Kata #13: Grasshopper - Summation
Link to Kata #14: Get the Middle Character
Link to Kata #15: Descending Order
Link to Kata #16: String ends with?
Link to Kata #17: Sum of two lowest positive integers
Link to Kata #18: Sum of odd numbers
Link to Kata #19: Find the next perfect square!
Link to Kata #20: Reversed Strings
Link to Kata #21: Jaden Casing Strings
Link to Kata #23: Returning Strings
Link to Kata #24: Opposite number
Link to Kata #25: Are You Playing Banjo?
Link to Kata #26: Beginner Series #1 School Paperwork