Solving FreeCodeCamp  Intermediate Algorithm Scripting Challenges

Solving FreeCodeCamp Intermediate Algorithm Scripting Challenges

Algo.jpg Klara Kulovec/EyeEm/Getty Images

P.S This blog post is a Javascript beginner's way of tackling some FreeCodeCamp challenges, the solution might seem a little bit rusty, kindly endeavor to leave a comment as this will go a long way to help me and others who might be tackling the challenge seeking a better and more efficient solution.

It's no doubt Algorithm is everywhere, its safe to say that everything in the world functions on one algorithm or the other. Algorithms can be referred to as a set of instruction(s) that are written or put in place to solve specific problems or accomplish specific tasks.

In this series, I will be documenting my code on how I solve some of the challenges on the Intermediate Algorithm scripting section of Freecodecamp.

Let's dive into the first challenge

Sum All Numbers in a Range

This challenge requires that we pass an array of two numbers. Then we are to return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.

I attempted solving it this way

function sumAll(arr) {
const max = arr.reduce(function(a, b) {
    return Math.max(a, b);
});

const min = arr.reduce(function(a, b) {
    return Math.min(a, b);
});

let sum = 0;
for(let i = min; i <= max; i++){
sum += i;
}
console.log(sum)
return sum;
}

sumAll([1, 4]); //This should return 10

Cover Story Image Source: Stephotec