Skip to content

Instantly share code, notes, and snippets.

@Shiggiddie
Last active August 29, 2015 14:07
Show Gist options
  • Save Shiggiddie/c85b4c438ddc9d12086d to your computer and use it in GitHub Desktop.
Save Shiggiddie/c85b4c438ddc9d12086d to your computer and use it in GitHub Desktop.
// Sum of a Range
function range (start, finish, step) {
if (step === undefined) {
step = 1;
}
array = [];
var reverse = false;
step = Math.abs(step)
console.log(start, finish, step);
if (start > finish){
var temp = start;
start = finish;
finish = temp;
reverse = true;
}
console.log(start, finish, step);
for (var value=start; value <= finish; value += step){
array.push(value);
}
if (reverse){
array.reverse();
}
console.log(array);
return array;
}
function sum (array) {
sum = 0;
console.log(array);
for (var index=0; index < array.length; index++){
sum += array[index];
}
return sum;
}
console.log(sum(range(1, 10)));
// → 55
console.log(range(5, 2, -1));
// → [5, 4, 3, 2]
// Reversing an Array
function reverseArray(array) {
return_array = []
for(var index=array.length-1; index >= 0; index--){
return_array.push(array[index]);
}
return return_array
}
function reverseArrayInPlace(array) {
for(var index=0; index < (Math.ceil(array.length/2)); index++){
fr_v = array[index];
bk_v = array[array.length-index-1];
array[index] = bk_v
array[array.length-index-1] = fr_v
}
}
console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
// A list
function arrayToList(array){
if (array.length == 0) {
return null;
}
return {value: array.shift(), rest: arrayToList(array)}
}
function listToArray(list){
array = [];
for (var node = list; node; node = node.rest){
array.push(node.value);
}
return array;
}
function prepend(value, list){
array = listToArray(list);
array.unshift(value);
return arrayToList(array);
}
function nth(list, pos){
array = listToArray(list);
return array[pos];
}
console.log(arrayToList([10, 20]));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(listToArray(arrayToList([10, 20, 30])));
// → [10, 20, 30]
console.log(prepend(10, prepend(20, null)));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(nth(arrayToList([10, 20, 30]), 1));
// → 20
// Deep Comparison
function isObjectAndNotNull(x) {
return (typeof(x) == "object" && x != null);
}
function deepEqual(obj1, obj2) {
if (obj1 === obj2) {
return true
}
else if (isObjectAndNotNull(obj1) && isObjectAndNotNull(obj2)) {
if (Object.keys(obj1).length == Object.keys(obj2).length) {
for (key in obj1) {
if (obj2.hasOwnProperty(key)) {
return deepEqual(obj1[key], obj2[key]);
}
else {
return false;
}
}
}
else {
return false;
}
}
else {
return obj1 == obj2;
}
}
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment