DEV Community

TakDevelops
TakDevelops

Posted on

Career Pivot into Development Journal, Day 6: JS Practice 5 – sumAll.js

It's been a few days, I've been quite busy with work so I had to prioritize that first, but I'm so happy I can get back to programming again! :D Moving on to the next exercise...


Understanding the problem

In this exercise, I have to create a function that takes two numbers as parameters, and sums all of the numbers between those two numbers.

Plan

  • Does the program have a UI? What will it look like? What functionality will it have? No UI
  • What are the inputs? Will the user enter in data or will you get it from somewhere else? Two numbers in the function parameter. The numbers will be entered when calling the function
  • What are the outputs? A number, which is the sum of all incremental whole numbers between the two parameters

Pseudocode

Declare a function that takes two parameters, `startNum` and `endNum`. Name it `sumAll`

Create a loop where the initialiser = `startNum` and the condition = `endNum`. Increment the loop.

Declare a variable `sum` and set it equal to 0

Concatenate `i` with `i` and store it in the variable `sum`

Return `sum`

Call the function `sumAll(startNum, endNum)`
Enter fullscreen mode Exit fullscreen mode

Divide & Conquer

Declare a function that takes two parameters, startNum and endNum. Name it sumAll

const sumAll = function(startNum, endNum) {}
Enter fullscreen mode Exit fullscreen mode

Create a loop where the initialiser = startNum and the condition = endNum. Increment the loop.

const sumAll = function(startNum, endNum) {
    for (let i = startNum; i <= endNum; i++) {
}
Enter fullscreen mode Exit fullscreen mode

Declare a variable sum and set it equal to 0

const sumAll = function(startNum, endNum) {
    let sum = 0;
    for (let i = startNum; i <= endNum; i++) {}
}
Enter fullscreen mode Exit fullscreen mode

Concatenate i with i and store it in the variable sum

const sumAll = function(startNum, endNum) {
    let sum = 0;
    for (let i = startNum; i <= endNum; i++) {
        sum += i;
}
}
Enter fullscreen mode Exit fullscreen mode

Return sum

const sumAll = function(startNum, endNum) {
    let sum = 0;
    for (let i = startNum; i <= endNum; i++) {
        sum += i;
    }
    return sum;
};
Enter fullscreen mode Exit fullscreen mode

Call the function sumAll(startNum, endNum)

const sumAll = function(startNum, endNum) {
    let sum = 0;
    for (let i = startNum; i <= endNum; i++) {
        sum += i;
    }
    return sum;
};

sumAll(1,5);
Enter fullscreen mode Exit fullscreen mode

Putting it to the test

There are 6 tests that I need to pass:

  • sums numbers within the range (1 ms)
  • Works with large numbers
  • Works with larger number first (1 ms)
  • Returns ERROR with negative numbers
  • Returns ERROR with non-number parameters
  • Returns ERROR with non-number parameters (1 ms)

Let's tackle these, starting from all the tests that should return ERROR. The first two tests have already passed based on our code above. To pass the rest of the tests, we need to use if...else statements in our sumAll function

Returns ERROR with negative numbers

if (startNum < 0 || endNum < 0) {
        return 'ERROR';
    }
Enter fullscreen mode Exit fullscreen mode

Returns ERROR with non-number parameters

if (startNum < 0 || endNum < 0 || typeof(startNum) !== 'number' || typeof(endNum) !== 'number') {
        return 'ERROR';
    }
Enter fullscreen mode Exit fullscreen mode

Works with larger number first (1 ms)

Working with larger number first means startNum > endNum. For this, we need to reverse the loop that we had above. Instead of incrementing, we will decrement

if (startNum < 0 || endNum < 0 || typeof(startNum) !== 'number' || typeof(endNum) !== 'number') {
        return 'ERROR';
    } else if (startNum > endNum) {
        for (let i = startNum; i >= endNum; i--) {
            sum += i;
        }
        return sum
    }
Enter fullscreen mode Exit fullscreen mode

Finally, we add in the incremental for loop we had above

const sumAll = function(startNum, endNum) {
    let sum = 0;
    if (startNum < 0 || endNum < 0 || typeof(startNum) !== 'number' || typeof(endNum) !== 'number') {
        return 'ERROR';
    } else if (startNum < endNum) {
        for (let i = startNum; i <= endNum; i++) {
            sum += i;
        }
        return sum;
    } else if (startNum > endNum) {
        for (let i = startNum; i >= endNum; i--) {
            sum += i;
        }
        return sum
    }
};
Enter fullscreen mode Exit fullscreen mode

✓ sums numbers within the range (1 ms)
✓ works with large numbers
✓ works with larger number first
✓ returns ERROR with negative numbers
✓ returns ERROR with non-number parameters
✓ returns ERROR with non-number parameters

All test have passed! On to the next exercise...

Top comments (0)