Skip to content

Instantly share code, notes, and snippets.

@jacretney
Last active June 6, 2020 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacretney/d7d8321eca82070be44f6581083e72b5 to your computer and use it in GitHub Desktop.
Save jacretney/d7d8321eca82070be44f6581083e72b5 to your computer and use it in GitHub Desktop.
A cursed Javascript loop to print out array items
/**
* A simple for loop to print out array items
*/
const TRUE = true;
const FALSE = !TRUE;
const zero = 0;
const one = increment(zero);
const items = ['banana', 'apple', 'eggplant'];
loop(items, function(value) {console.log(value)});
function loop(arr, callback, count, loops) {
const limit = length(items);
if (isFalse(count)) {
count = zero;
}
if (isFalse(loops)) {
loops = zero;
}
if (isntFalse(count < limit)) {
callback(arr[count], loops);
loop(arr, callback, increment(count), increment(loops));
}
}
function length(arr) {
var temp = makeNewArray(arr);
var x = zero;
while (isntFalse(temp[zero])) {
x = increment(x);
temp = shiftArray(temp);
};
return x;
}
function makeNewArray(arr) {
var newArr = [];
var count = zero;
var shouldContinue = TRUE;
do {
if(arr[count]) {
newArr[count] = arr[count];
count = increment(count);
} else {
shouldContinue = FALSE;
}
} while(isntFalse(shouldContinue));
return newArr;
}
function increment(value) {
return value + 1;
}
function decrement(value) {
return value - one;
}
function isntFalse(value) {
if (value) {
return TRUE;
}
return FALSE;
}
function isFalse(value) {
if (!isntFalse(value)) {
return TRUE;
}
return FALSE;
}
function shiftArray(arr) {
var newArr = [];
var count = 1;
var shouldContinue = TRUE;
do {
if(arr[count]) {
newArr[decrement(count)] = arr[count];
count = increment(count);
} else {
shouldContinue = FALSE;
}
} while(isntFalse(shouldContinue));
return newArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment