Codewars – Beginner Series #3 Sum of Numbers

Salutations.

I’m posting Codewars challenges and my thought process in this series. I’m using JS and Node 18 whenever possible. Just for the sake of clarity, I’m making fair use of them.

So, next in this series is Sum of Numbers. In this specific p…


This content originally appeared on DEV Community and was authored by Nicolás Bost

Salutations.

asdf turtle

I'm posting Codewars challenges and my thought process in this series. I'm using JS and Node 18 whenever possible. Just for the sake of clarity, I'm making fair use of them.

So, next in this series is Sum of Numbers. In this specific problem, it's more math than coding. See, you need to calculate an area. In this graph, for example, we show all values between -5 and 4:

Desmos graph

You can use integration if you so desired, but there's a simpler route. Since we're dealing with linear functions, we can search for the median and multiply for the range:

sum=median∗rangesum = median * rangesum=medianrange

sum=b−b−a2∗(b−a+1)sum = b - \frac{b-a}{2} * (b - a + 1)sum=b2ba(ba+1)

sum=b2−a2+b+a2sum = \frac{b^2 - a^2 + b + a}{2}sum=2b2a2+b+a

So we just need to insert that equation in the code. It starts like this:

function getSum(a, b)
{
   //Good luck!
}
function getSum(a, b)
{
   let sum = (b ** 2 - a ** 2 + b + a ) / 2 ;
   return sum;
}

We test it and:

Poh frustration

But why? I know the equation is correctly simplified, so... Oh. This is the problem:

getSum(a, b)

(a,b) in exactly that order. It works if the input is (-5,4), but not if it's (4,-5). Fix? You could code an "if" statement for both situations. I won't do it like that though. I'll do this:

if (a > b){
    let c = a;
    a = b;
    b = c;
  }

And so, we put together everything:

function getSum(a, b)
{
  if (a > b){
    let c = a;
    a = b;
    b = c;
  }
  let sum = (b ** 2 - a ** 2 + b + a ) / 2 ;
  return sum;
}

Somewhat decent, easy for the reading.

Cya. Drink water 💧💧💧.

Previous


This content originally appeared on DEV Community and was authored by Nicolás Bost


Print Share Comment Cite Upload Translate Updates
APA

Nicolás Bost | Sciencx (2025-01-03T02:46:27+00:00) Codewars – Beginner Series #3 Sum of Numbers. Retrieved from https://www.scien.cx/2025/01/03/codewars-beginner-series-3-sum-of-numbers-2/

MLA
" » Codewars – Beginner Series #3 Sum of Numbers." Nicolás Bost | Sciencx - Friday January 3, 2025, https://www.scien.cx/2025/01/03/codewars-beginner-series-3-sum-of-numbers-2/
HARVARD
Nicolás Bost | Sciencx Friday January 3, 2025 » Codewars – Beginner Series #3 Sum of Numbers., viewed ,<https://www.scien.cx/2025/01/03/codewars-beginner-series-3-sum-of-numbers-2/>
VANCOUVER
Nicolás Bost | Sciencx - » Codewars – Beginner Series #3 Sum of Numbers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/03/codewars-beginner-series-3-sum-of-numbers-2/
CHICAGO
" » Codewars – Beginner Series #3 Sum of Numbers." Nicolás Bost | Sciencx - Accessed . https://www.scien.cx/2025/01/03/codewars-beginner-series-3-sum-of-numbers-2/
IEEE
" » Codewars – Beginner Series #3 Sum of Numbers." Nicolás Bost | Sciencx [Online]. Available: https://www.scien.cx/2025/01/03/codewars-beginner-series-3-sum-of-numbers-2/. [Accessed: ]
rf:citation
» Codewars – Beginner Series #3 Sum of Numbers | Nicolás Bost | Sciencx | https://www.scien.cx/2025/01/03/codewars-beginner-series-3-sum-of-numbers-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.