////////////////////////////////////// // Max in Array ////////////////////////////////////// function max(arr) { var m; for(var i=0; i < arr.length; i++) { if(i==0) { m = arr[i]; continue; } if(arr[i] > m) { m = arr[i]; } } return m; } var myArray = [-1,-2,-3,100,5]; log("Max in Array"); log(max(myArray)); log("Max in Array with library"); log(Math.max.apply(null,myArray)); ////////////////////////////////////// // LINKED LIST ////////////////////////////////////// function LinkedList() {} LinkedList.prototype = { length: 0, head: null, tail: null }; LinkedList.Node = function(data) { this.prev = null; this.next = null; this.data = data; }; LinkedList.prototype.append = function(node) { if (this.head === null) { node.prev = null; node.next = null; this.head = node; this.tail = node; } else { this.tail.next = node; this.prev = this.tail; this.tail = node; } this.length++; }; var myList = new LinkedList(); myList.append(new LinkedList.Node("a")); myList.append(new LinkedList.Node("b")); myList.append(new LinkedList.Node("c")); log("Linked list"); var node = myList.head; for(var i=0; i<myList.length; i++) { log(node.data); node = node.next; } log("Linked list reveresed"); reverseLinkedList(myList); var node1 = myList.head; for(var i=0; i<myList.length; i++) { log(node1.data); node1 = node1.next; } function reverseLinkedList (linkedList) { var temp = null; var current = linkedList.head; // swap next and prev for all nodes of doubly linked list while (current != null) { current.prev = current.next; current.next = temp; temp = current; current = current.prev; } linkedList.head = temp; } ////////////////////////////////////// // fibonacci - Fn = Fn-1 + Fn-2 //F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 // 0 1 1 2 3 5 8 13 21 34 55 89 ////////////////////////////////////// function fibonacci(n) { if (n <= 1) return n; return fibonacci(n - 2) + fibonacci(n - 1); } fibonacciCache = (function () { var cache = { 0:0, 1:1 }; return function (n) { var cached = cache[n]; if (typeof cached !== 'undefined') return cached; return (cache[n] = fibonacciCache(n - 2) + fibonacciCache(n - 1)); }; }()); log("Fibonacci of 11"); log(fibonacci(11)); log("fibonacci of 25 with cache"); log(fibonacciCache(25)); ////////////////////////////////////// // Pub sub ////////////////////////////////////// /* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011 * http://benalman.com/ * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */ (function($) { var o = $({}); $.subscribe = function() { o.on.apply(o, arguments); }; $.unsubscribe = function() { o.off.apply(o, arguments); }; $.publish = function() { o.trigger.apply(o, arguments); }; }(jQuery)); log("Pub / sub"); $.subscribe("/some/topic", function(e, a, b, c) { // subscrbe log(a + b + c); }); $.subscribe("/some/topic", function(e, a, b, c) { // subscrbe log("Subscriber 2: " + a + b + c); }); $.publish("/some/topic", [ "a", "b", "c" ]); // publish $.unsubscribe("/some/topic"); // Unsubscribe all handlers for this topic ////////////////////////////////////// // Helpers ////////////////////////////////////// function log(m) { $("#info").append(m + "<br/>"); }
JavaScript Samples <div id="info"></div>