Just when you though JavaScript couldn’t get any more weird, you discover the world of algorithms!

a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer. — Dictionary

In programming terms, an algorithm are often functions, that can be referenced by a larger program. These functions can have simple operations like, addition, subtraction and multiplication, along with complicated operations like find the greatest integer and return that integer in a new array times itself! Sounds crazy right!? Well Sometimes Algorithms can get a little overwhelming the more complicated they are!

A few tips that I have learned from practicing a few simple algorithms are:

  • Read the material carefully, throughly and repeatedly! Nothing is worst than skimming through a problem and thinking you “got it”, only to realize you misinterpreted what it wants you to do!
Discovering JS Algorithms

Here is an example of a simple Algorithm I did months ago while in the first stages of learning how to code:

Write a function called min, which accepts an array of numbers and returns the lowest value.

Do note use the built-in Math.min() function!

min([5, 1, 4, 7, 1, 2]); // 1
min([-1, 6, 3, 2.2, -10, -4]); // -10
function min(arr){
//can use first element as default 'minimum'
let minimum = arr[0];
//then if we encounter a smaller element in the rest of the array,
// replace the value. Note we can start our loop at index 1
for (var i = 1; i < arr.length; i++){
//difference here is if our arr[i] is less than the minimum variable, we assign minimum to that value
if(arr[i] < minimum){
minimum = arr[i];
return minimum;
min([22, 44, 3, 5,77]) // => 3

Let us break this down into pieces:

  • First we created our function that takes in the parameter of an array

Algorithm curtesy of Rithm School

Software Engineer