Everyone Can Code!

JavaScript Kata #18: Sum of odd numbers

By YayoDrayber | Learn To Code With Me | 26 Aug 2022


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 #18

DESCRIPTION:

Given the triangle of consecutive odd numbers:

             1
          3     5
       7     9    11
   13    15    17    19
21    23    25    27    29
...

Calculate the sum of the numbers in the nth row of this triangle (starting at index 1) e.g.: (Input --> Output)

1 -->  1
2 --> 3 + 5 = 8

Starting code:

function rowSumOddNumbers(n) {
	// TODO
}

My attempt that worked:

function rowSumOddNumbers(n) {
  return n**3
}

Shortened it to this:

const rowSumOddNumbers = n => n**3

Almost went crazy thinking about how to solve this. I was initially thinking, "maybe I should simulate the triangle first, and construct an array within an array" but I didn't know how to do that yet. Looked at the triangle again to try to see a pattern then realized maybe I'm overthinking this too much. Got the overthinking confirmation when I saw rank #1 in best practices:

function rowSumOddNumbers(n) {
  return Math.pow(n, 3);
}

Rank #2 comes with the explanation for the pattern as to why the solution code is so short, thanks to user DrFrankenstein for this submission:

function rowSumOddNumbers(n)
{
  /* The rows' start numbers are Hogben's centered polygonal numbers:
     1, 3, 7, 13, 21, 31, 43 = b[n] = n^2 - n + 1.
     <https://oeis.org/A002061>
     
     The sum of one row is given by:
     s[n] = n^2 + n(b[n] - 1).
     <https://www.quora.com/What-is-the-sum-of-n-consecutive-odd-integers/answer/Xavier-Dectot>
     
     Inline b[n]:
     s[n] = n^2 + n(n^2 - n + 1 - 1)
          = n^2 + n(n^2 - n)
          = n^2 + n^3 - n^2
          = n^3
     ... oh. */
  return n * n * n;
}

Same approach with rank #3 and #4, while rank #5 shows how I almost did my solution in exasperation (thanks to users suadev, VictoriaV, BryanParcia, Marksteven, anwer bargui for this submission):

function rowSumOddNumbers(n) {
  var start = n * n - n + 1;
  var result = 0;  
  
  for(i = 0; i < n; i++)
  {
    result =  result + (start + (i*2));
  }
    
   return result;
}

That's it for Kata #18, 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

Resources

  1. https://www.publish0x.com/learn-to-code-with-me/obligatory-introductory-post-xddgyxl
  2. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-1-squaren-sum-xvmywpl
  3. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-2-convert-a-number-to-a-string-xmymkwm
  4. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-3-dna-to-rna-conversion-xgjlpox
  5. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-4-remove-first-and-last-character-xyeyykj
  6. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-5-makeuppercase-xzgnnqd
  7. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-6-total-amount-of-points-xeellvx
  8. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-7-a-needle-in-the-haystack-xlqzzgp
  9. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-8-sum-of-positive-xxzyyrl
  10. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-9-basic-mathematical-operations-xlqzzer
  11. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-10-beginner-reduce-but-grow-xxzyylv
  12. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-11-square-every-digit-xnnxxyr
  13. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-12-friend-or-foe-xddggmm
  14. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-13-grasshopper-summation-xnnxxwr
  15. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-14-get-the-middle-character-xrgnney
  16. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-15-descending-order-xmymmwn
  17. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-16-string-ends-with-xeelnpk
  18. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-17-sum-of-two-lowest-positive-integers-xjroxpn

How do you rate this article?

1


YayoDrayber
YayoDrayber

A Yayo, A Drayber, among others.


Learn To Code With Me
Learn To Code With Me

Just started with Codewars and will be posting the solutions I came up with here and the apparent best practices.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.