DEV Community

Sam
Sam

Posted on

Psuedo-code is so important!

I am proud of what I accomplished in learning Test Driven Development as I self-teach myself JavaScript. Better late than never. Continuing in exercises that I initially mentioned in an earlier article, this time the exercise is sumAll where you have a beginning number and ending number and wanting to add all the numbers in between, including the end and starting points.

What I did first was do psuedo-code because I keep being told how it makes things a lot more clear and I am glad I did because I knew what I wanted in human language, and need to translate to computer.

I admit, sometimes I feel the urge of just doing the programming and make it perfect as I can because that was what I was taught in classes. But, I am starting to see how people are right that it's better to plan and break down the problems one by one. And I usually run into code-blocks and get myself frustrated and give up. But this problem I didn't feel any frustration when I wrote down what I want, variables I want to name, like naming ingredients for a recipe! I found myself referring back to my human notes each time I felt like I wasn't getting somewhere and utilizing Stack Overflow and google to learn of functions like instanceof that can do checking of parameters being an array or not.

  //What code should accomplish
  //Want two variables, numStart and numEnd
  //Loop to begin at numStart and end at numEnd
  //Add numbers between numStart and numEnd, including the two in summation
  //return the sum at the end, variable named finalSum
  //check if parameters are negative or non-number as well
Enter fullscreen mode Exit fullscreen mode

Then it was a matter of getting right output, and I had quite a few console.log to check :) I almost got stuck at part where larger number is started first, but then realized it's the same logic, except we would be decrementing instead of incrementing.

The complete code is as follows:

const sumAll = function (numStart, numEnd) {

    let finalSum = 0; //holds the sum of numbers
    let addedNumbers = 0; //hold added numbers
    let i = numStart

    //Checking right away to see if number is non-number or negative.
    if (i < 0 || typeof numStart != "number" || typeof numEnd != "number" ||
        numStart instanceof Array || numEnd instanceof Array) {
        return 'ERROR';
    }

    //handles case when numStart is larger
    while (i > numEnd) {
        addedNumbers += i;
        i--;

        if (i == numEnd) {
            addedNumbers += i;
            finalSum = addedNumbers;
            console.log('#2 Final sum is: ', finalSum);
        }
    }

    //handles case when numStart is smaller
    while (i < numEnd) {
        addedNumbers += i;
        i++;

        if (i == numEnd) {
            addedNumbers += i;
            finalSum = addedNumbers;
            console.log('Final sum is: ', finalSum);
        }
    }
    return finalSum

};


// Do not edit below this line
module.exports = sumAll;
Enter fullscreen mode Exit fullscreen mode

I say all of this to document my journey and look back when I improve and become a senior in this field I chose to pursue. Although I feel disheartened and sad over how I cannot truly enjoy the holidays as I should because I cannot afford to do so, one day I will have my Tech career providing me the accessibility to needs and financial stability that will allow me to have fun and feel accomplished.

Each day is a step to that brighter tomorrow, even though I may walk in darkness. This is my testimony of a rising Computer Engineer.

Image of sailor moon with her fist balled up in a stance of determination

Oldest comments (2)

Collapse
 
puemos profile image
Shy Alter

That is great!
In addition, it's critical to keep the feedback loop short, try to maintain a good balance between upfront thinking and development. A great heuristic might be, "Can I learn something new?" or "Do I have enough info to start implementing?".
Good luck with your journey :)

Collapse
 
vickilanger profile image
Vicki Langer

I’m so glad you’ve found the usefulness of pseudocode. I was apprehensive to use it when I started and now it’s my first step. I even wrote about it to try helping others.