This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Welcome to the JavaScript Visual Interpreter! | |
It can help you understand exactly how | |
and why your code executes the way that it does. | |
Try typing a snippet of JavaScript here | |
stepping through each operation with the buttons above | |
--or to get started quickly, | |
try one of the code snippets in the list below. | |
You can check out the code at https://github.com/visualinterpreter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var applyResult = 0; | |
var callResult = 0; | |
var sum = function(a, b, c){ | |
return a + b + c; | |
}; | |
applyResult = sum.apply(null, [1,2,3]); // returns 6 | |
callResult = sum.call(null, 1, 2, 3); //also returns 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var f = []; | |
var factorial = function (n) { | |
if (n === 0 || n === 1){ | |
return 1; | |
} | |
if (f[n] > 0){ | |
return f[n]; | |
} | |
f[n] = factorial(n-1) * n; | |
return f[n]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fibonacci = function(n, output) { | |
var a = 1, b = 1, sum; | |
for (var i = 0; i < n; i++) { | |
output.push(a); | |
sum = a + b; | |
a = b; | |
b = sum; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var is_even = function(a){ | |
if (a === 0){ | |
return true; | |
}else{ | |
return is_odd(a - 1, 'testb'); | |
} | |
}; | |
var is_odd = function(b){ | |
if (b === 0){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var trampoline = function(fun){ | |
var args = Array.prototype.slice.call(arguments,1); | |
var result = fun.apply(this, args); | |
while(typeof result === 'function') { | |
result = result(); | |
} | |
return result; | |
}; | |
var countDown = function(n){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bubbleSort = function(array){ | |
for(var i = 0; i < array.length-1 ; i++){ | |
for(var j = 0; j < array.length-1 ; j++ ){ | |
if(array[j]>array[j+1]){ | |
var swap = array[j]; | |
array[j] = array[j+1]; | |
array[j+1] = swap; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A prime number is a whole number that has no other divisors other than | |
* itself and 1. Write a function that accepts a number and returns true if it's | |
* a prime number, false if it's not. | |
*/ | |
var primeTester = function(n) { | |
if( n === 1) return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var longestPalindrome = function (string) { | |
// Your code here | |
// Sliding window problem. | |
// If string is palindrom, return string | |
// else check if substring of length stringLength-n is palindrome. | |
// As soon as you get a palindrome you've found the longest palindrome | |
// since you're shrinking the window and you want the longest one. | |
var stringLength = string.length | |
var n = 0; | |
while( n < stringLength){ |
OlderNewer