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 #32
DESCRIPTION:
Task:
Your task is to write a function which returns the sum of following series upto nth term(parameter).
Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +...
Rules:
-
You need to round the answer to 2 decimal places and return it as String.
-
If the given value is 0 then it should return 0.00
-
You will only be given Natural Numbers as arguments.
Examples:(Input --> Output)
1 --> 1 --> "1.00"
2 --> 1 + 1/4 --> "1.25"
5 --> 1 + 1/4 + 1/7 + 1/10 + 1/13 --> "1.57"
Starting code:
function SeriesSum(n)
{
// Happy Coding ^_^
}
My attempt:
function SeriesSum(n){
let seriesN = 1
let result = 0
for(i=0;i<n;i++){
result += 1/seriesN
seriesN +=3
}
return result.toFixed(2)
}
Rank #1 in 'Best Practice':
function SeriesSum(n) {
for (var s = 0, i = 0; i < n; i++) {
s += 1 / (1 + i * 3)
}
return s.toFixed(2)
}
Rank #2:
function SeriesSum(n) {
var sum = 0;
for(var i = 0; i < n; i++) {
sum += 1 / (3 * i + 1);
}
return sum.toFixed(2);
}
Rank #3:
function SeriesSum(n)
{
for(a=0,i=1;i<=n*3;i+=3) a+=1/i;
return a.toFixed(2);
}
Rank #4:
function SeriesSum(n, s = 0) {
return n ? SeriesSum(n - 1, s + 1 / (3 * n - 2)) : s.toFixed(2)
}
Rank #5 with the array conversion and reduce:
function SeriesSum(n) {
return Array(n).fill(0).map((e, i) => 3 * i + 1).reduce((s, e) => s + 1 / e, 0).toFixed(2);
}
Rank #6:
function SeriesSum(n)
{
var s = 0;
while(n) {
s += 1/(1 + 3 * --n);
}
return s.toFixed(2);
}
Not sure what's happening at rank #7 but from what I've skimmed in the resources so far, lodash is a JavaScript library and based on the context, range() is a function from that library which probably works as a replacement for the loop:
const { range } = require('lodash')
const reducer = (a, i) => a + 1 / (1 + i * 3)
const SeriesSum = (n) => range(n).reduce(reducer, 0).toFixed(2)
Way down at rank #8 is the one-liner by user e.mihaylin:
SeriesSum = n => Array.from(Array(n), (e, i) => 1 / (i * 3 + 1)).reduce((s, e) => s + e, 0).toFixed(2);
Thanks to this challenge, I am now at 6 kyu. Feels like I'm getting closer and closer to my goal yet I don't feel any smarter. 🤣
That's it for Kata #32, 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
Link to Kata #27: Remove String Spaces
Link to Kata #28: Invert values