Beginner Series #3 Sum of Numbers

Instructions: Given two integers a and b, which can be positive or negative, find the sum of all the integers between and including them and return it. If the two numbers are equal return a or b. Note: a and b are not ordered! Examples (a, b) --> output (explanation) (1, 0) --> 1 (1 + 0 = 1) (1, 2) --> 3 (1 + 2 = 3) (0, 1) --> 1 (0 + 1 = 1) (1, 1) --> 1 (1 since both are same) (-1, 0) --> -1 (-1 + 0 = -1) (-1, 2) --> 2 (-1 + 0 + 1 + 2 = 2) Your function should only return a number, not the explanation about how you get that number. Thoughts: I first check if a and b are equal to quickly return a result when the condition is met. Otherwise the code will check which number is smaller(Math.min(a,b)) and larger(Math.max(a,b)) and add it trough a sum, starting from the smaller integer to the larger one. The tests allowed me to use this method as a and b are small numbers and all the numbers between them are integers. Solution: This is a CodeWars Challenge of 7kyu Rank (https://www.codewars.com/kata/55f2b110f61eb01779000053/javascript)

Mar 22, 2025 - 05:12
 0
Beginner Series #3 Sum of Numbers

Instructions:
Given two integers a and b, which can be positive or negative, find the sum of all the integers between and including them and return it. If the two numbers are equal return a or b.

Note: a and b are not ordered!

Examples (a, b) --> output (explanation)
(1, 0) --> 1 (1 + 0 = 1)
(1, 2) --> 3 (1 + 2 = 3)
(0, 1) --> 1 (0 + 1 = 1)
(1, 1) --> 1 (1 since both are same)
(-1, 0) --> -1 (-1 + 0 = -1)
(-1, 2) --> 2 (-1 + 0 + 1 + 2 = 2)
Your function should only return a number, not the explanation about how you get that number.

Thoughts:

  1. I first check if a and b are equal to quickly return a result when the condition is met.
  2. Otherwise the code will check which number is smaller(Math.min(a,b)) and larger(Math.max(a,b)) and add it trough a sum, starting from the smaller integer to the larger one.
  3. The tests allowed me to use this method as a and b are small numbers and all the numbers between them are integers.

Solution:
Solution

Tests
This is a CodeWars Challenge of 7kyu Rank (https://www.codewars.com/kata/55f2b110f61eb01779000053/javascript)