Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CharmedSatyr
CharmedSatyr / deduplicate_array.js
Last active July 28, 2018 00:00
Deduplicate Array
// METHOD 1: reduce/indexOf
//`uniques` contains one of each unique value in `arr`
let arr = ['ant', 0, 'aunt', 'ant'];
let uniques = arr.reduce((acc, curr) => {
if (acc.indexOf(curr) < 0) {
acc.push(curr);
}
return acc;
}, []);
@CharmedSatyr
CharmedSatyr / fibonacci_number.js
Last active August 1, 2018 00:57
Fibonacci Number Finder algorithms, one recursive and one iterative, implemented in JavaScript
// Fibonacci Number
// Objective: Find the n-Fibonacci number,
// the nth term of the Fibonacci Sequence,
// often denoted by F(n)
// For example, given the Fibonacci Sequence:
// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377...
// F(8) = 21
// F(20) = 6765
@CharmedSatyr
CharmedSatyr / merge_sort.js
Last active August 1, 2018 03:45
Merge Sort implemented in JavaScript
/* Like quick sort, merge sort also uses a divide-and-conquer,
* recursive methodology to sort an array. It takes advantage
* of the fact that it is relatively easy to merge two arrays
* in sorted order as long as each is sorted in the first place.
* 1) Recursively split the input array in half until a sub-array
* with only one element is produced.
* 2) Merge each sorted sub-array together to produce the final
* sorted array.
*/
@CharmedSatyr
CharmedSatyr / quick_sort.js
Last active August 1, 2018 03:57
Quick Sort implemented in JavaScript
/* Quick sort is an efficient, recursive divide-and-conquer
* approach to sorting an array. In this method, a pivot value
* is chosen in the original array. The array is then
* partitioned into two subarrays of values less than and
* greater than the pivot value. We then combine the result of
* recursively calling the quick sort algorithm on both
* sub-arrays. This continues until the base case of an empty
* or single-item array is reached, which we return. The
* unwinding of the recursive calls return us the sorted array.
*/
@CharmedSatyr
CharmedSatyr / selection_sort.js
Last active August 1, 2018 04:06
Selection Sort implemented in JavaScript (2 methods)
/* Selection sort works by selecting the minimum value in a list
* and swapping it with the first value in the list. It then
* starts at the second position, selects the smallest value in
* the remaining list, and swaps it with the second element. It
* continues iterating through the list and swapping elements
* until it reaches the end of the list.
*/
// METHOD 1: Using 2 `for` loops (ES5)
function selectionSort(array) {
@CharmedSatyr
CharmedSatyr / bubble_sort.js
Last active August 1, 2018 04:10
Bubble Sort implemented in JavaScript
/* The bubble sort method starts at the beginning of
* an unsorted array and 'bubbles up' unsorted values
* towards the end, iterating through the array until
* it is completely sorted. It does this by comparing
* adjacent items and swapping them if they are out
* of order. The method continues looping through the
* array until no swaps occur, at which point the
* array is sorted.
*/
@CharmedSatyr
CharmedSatyr / insertion_sort.js
Last active August 1, 2018 04:14
Insertion Sort implemented in JavaScript
/* This method works by building up a sorted array at the beginning of
* the list. It begins the sorted array with the first element. Then it
* inspects the next element and swaps it backwards into the sorted
* array until it is in sorted position. It continues iterating through
* the list and swapping new items backwards into the sorted portion
* until it reaches the end.
*/
'use strict';
// insert - puts a number in the right spot in an array in ascending order
@CharmedSatyr
CharmedSatyr / index.html
Last active August 1, 2018 15:01
Interview Question// JS Bin// source https://jsbin.com/wurebac
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
### Keybase proof
I hereby claim:
* I am charmedsatyr on github.
* I am charmedsatyr (https://keybase.io/charmedsatyr) on keybase.
* I have a public key ASAlfvhXnBEtxQECzQFYUMWRhL0wUBV1pvNmsRA9UXuMiAo
To claim this, I am signing this object:
@CharmedSatyr
CharmedSatyr / caesars-cipher.js
Created July 5, 2019 02:09
freeCodeCamp JavaScript Algorithms and Data Structures Projects
function rot13(str) {
// LBH QVQ VG!
//translate each letter of the string into unicode and push them to an array
var uniArr = [];
for (j = 0; j < str.length; j++) {
uniArr.push(str.charCodeAt(j));
}
//add or subtract 13 letters or do nothing, depending on where the initial value falls in the ISO Latin-1 table