Javascript Algorithms


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!
  • Pseudocode the problem before you code it out! I have made many mistakes not doing this because I read the problem and though “this seems straight forward enough for me to code out ”, only to be proven wrong! Always write out your thought process before you put your words into code!
  • Google is your best friend! Yes that’s right! Handy Dandy google is always there to help you! Not to look up the solution, but to help you find better alternative to solving the problem! What I mean by that is, in the event of the Algorithm saying “do this operation, but do not use this built in method”, well, if that built in method was your first initial thought, then you feel defeated already! Alternatively, there are always different ways to solve a problem without that built in method, You just have to google it!
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
  • We then define a variable minimum and assign it to array at the first index (which is also 0 index, programming code starts at 0).
  • Next, we use a for loop to cycle through our array of numbers. Note: we are not limited to just using a for loop to look through the whole array.
  • Our condition is , if the array at index [i] arr[i] is less than our minimum variable, we want that integer to be our new variable!
  • Lastly, we must return the minimum variable.

Algorithm curtesy of Rithm School



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store