thewhitefox

thewhitefox

# JS Algorithms

2021 January 7th

## Algorithms repeatable?

I love to cook, eat delicious food & learn new recipes. Algorithms for me are programming recipes. As with recipes I Cook, Eat, Tweak & Repeat this concept I follow with algoritms write, run, tweak, repeat.

In my humble opinion, a good way to improve one's cooking skills is to take those recipes and create your own either by tweaking or taking the concepts of what one has learned and applying them to different ingredients, in this case different languages.

As with my cooking, my programming will improve by learning algorithms and my problem solving, these are some JS algorithms that I will understand, not just do. JS(ES6)

In this post I chose JS as the language to learn the below algorithms because it's the language of the web and a language I want to master. Using Big O notation

"Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity." Wikipedia

## Algorithms:

1. Two Sum
2. Fibonacci - learn recursion
3. FizzBuzz - little fun test
4. Dijkstra - shortest path between nodes
5. Harmless Ransom Note use Big O notation
6. Palindrome - String & array manipulation - String and array methods
7. Caesar's Cipher - encryption
8. Reverse string

### Two Sum

Check array for sum of two values with Big O notation

This algorithm will be a JS `function` that takes two parameters, a numbers array `arr` and a sum value called `sum`. This `function` returns a of pair indices from `arr` that add up to the `sum` parameter.

This first attempt is using brute force O(n2). To test copy / paste `twoSumBrute(arr, 10);` The arr passed in has already been defined within the JS code as `const arr = [3, 7, 11, 15];`. You could also enter an array and a sum of your own like this example `twoSumBrute([9, 33, 50, 41], 59)`; If a sum is not in the array an error will the arr and sum passed in will be returned `Error no two numbers in array for sum: 159`

`1// Two Sum return indices O(n)22const arr = [3, 7, 11, 15]3  function twoSumBrute(arr, sum) {4}`

### Fibonacci

`1// Big O2const fibA = [1, 2, 3, 5, 8]3  function fibonacci() {4}`

## Reference

bigOcheatsheet

https://Rob Bell beginners guide to big o notation

Why are algorithms so important

stackoverflow fibonacci recursive

How to Implement Dijkstras

thatjsdude check Palindrome

learning JS algorithm from scratch

palindromelist

two ways to check for palindromes JS

medium Tim Severien substitution cipher JS

Reverse a string

You Dont Know JS

freecodecamp js arrayreverse tut

webpack Fetch APIReact Basics