Skip to content

Instantly share code, notes, and snippets.

View johnhutchins's full-sized avatar

John Hutchins johnhutchins

View GitHub Profile
@johnhutchins
johnhutchins / generateIntegerArray.js
Created April 27, 2020 13:06
generate array of numbers in between two numbers
function generateIntegers(m, n) {
let arr = []
let init = m
let length = (n - m) + 1
while(arr.length < length){
arr.push(init)
init = init + 1
}
return arr
function removeDuplicatetElevations(arr){
console.log("NON FILTERED === ", arr)
const filteredArr = arr.reduce((acc, current) => {
const x = acc.find(item => item['height'] === current['height']);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
@johnhutchins
johnhutchins / arrayToChunksOfArrays.js
Created December 9, 2019 15:23
take a large array and break into smaller chunks
function splitArrToSmallerChunks(bigArr){
let arrOfArr = []
while(bigArr.length){
arrOfArr.push(bigArr.splice(0,50))
}
return arrOfArr
}
@johnhutchins
johnhutchins / baseAjax.js
Created November 7, 2019 05:12
base requestAjax function
function requestAJAX(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
callback(JSON.parse(xhr.responseText))
}
}
xhr.open('GET', url, true)
xhr.send()
}
@johnhutchins
johnhutchins / factorial.js
Created October 16, 2019 00:11
get factorial of a number
function factorial(num){
let total = 1;
for(let i=1;i<=num;i++){
total *= i;
}
return total;
}
@johnhutchins
johnhutchins / randomNumbersInBetweenTwoNumbers.js
Created October 8, 2019 18:07
Get a random number in between two numbers
function randInBetweenNumbers(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
@johnhutchins
johnhutchins / middleNumberInBetweenTwoNumbers.js
Created October 8, 2019 18:06
Get the middle number between two numbers.
function middleInBetweenNumnbers(min, max) {
return Math.floor((max+min)/2);
}
@johnhutchins
johnhutchins / addItemsFromArr.js
Created September 25, 2019 00:21
adds items from a passed in arr and returns the total
function add(arr){
let total = 0;
arr.forEach(function(num){
console.log(num);
total += Number.parseInt(num);
});
console.log(total);
return total;
}
@johnhutchins
johnhutchins / isNumEvenByRecursion.js
Created September 24, 2019 15:07
recursion: find if number is even
function isEven(i){
function subtract(){
console.log(i - 2);
return i - 2;
}
if(i===0){ return true; }
if(i === 1){ return false; }
if(i <=0){
return i;
}
@johnhutchins
johnhutchins / isNumberEven.js
Created September 20, 2019 12:58
is a number even or odd
function isEven(i){
if(i % 2 === 0){
console.log(true);
} else{
console.log(false);
}
}isEven(-1);