Career Pivot into Development Journal, Day 6: JS Practice 5 – sumAll.js
Tags: webdev, beginners, codenewbie, devjournal
Published: 2022-03-31 04:47:39

_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)`
```

##Divide & Conquer
###Declare a function that takes two parameters, `startNum` and `endNum`. Name it `sumAll`
```
const sumAll = function(startNum, endNum) {}
```

###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++) {
}
```

###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++) {}
}
```

###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;
}
}
```

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

###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);
```

---

##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';
    }
```

###Returns ERROR with non-number parameters
```
if (startNum < 0 || endNum < 0 || typeof(startNum) !== 'number' || typeof(endNum) !== 'number') {
        return 'ERROR';
    }
```

###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
    }
```

---

**_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
    }
};
```

✓ 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..._**