This file contains 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
// Exercise 2-1 | |
// in console | |
// Exercise 2-2 | |
var init = 1; | |
var k = 1; | |
while (k++ <= 10) | |
init = init * 2; | |
print (init); | |
// Exercise 2-3 | |
var str = ""; |
This file contains 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
// Exercise 3.1 | |
function absolute (num) { | |
if (num >= 0) | |
return num; | |
return (- num); | |
} | |
// I realize that testing the other branch first is more efficient |
This file contains 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
//Ex. 4.2 | |
function range (num) { | |
var arr = []; | |
if (isNaN(Number(num))) | |
alert ("Invalid number"); | |
else | |
for (var i=0;i<num;i++) | |
arr[i]=i; | |
return arr; |
This file contains 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 test = function () { | |
function first () { | |
print ("first"); | |
} | |
function second () { | |
print ("second"); | |
} | |
}; | |
// add a function to Object.prototype | |
Object.prototype.getOwnMethods = function(){ |
This file contains 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
// coming soon | |
// Ex. 6.1 | |
function forEach(array, action) { | |
for (var i = 0; i < array.length; i++) | |
action(array[i]); | |
} | |
function reduce(combine, base, array) { | |
forEach(array, function (element) { |
This file contains 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
<script type="text/javascript"> | |
function walkTheDOM(node, func) { | |
func(node); | |
node = node.firstChild; | |
while (node) { | |
walkTheDOM(node, func); | |
node = node.nextSibling; | |
} | |
} | |
function countTextNodes() { |
This file contains 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
<script type="text/javascript"> | |
function walkTheDOM(node, func) { | |
func(node); | |
node = node.firstChild; | |
while (node) { | |
walkTheDOM(node, func); | |
node = node.nextSibling; | |
} | |
} | |
function countTextNodes() { |
This file contains 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
<script type="text/javascript"> | |
function walkTheDOM(node, func) { | |
func(node); | |
node = node.firstChild; | |
while (node) { | |
walkTheDOM(node, func); | |
node = node.nextSibling; | |
} | |
} | |
function countTextNodes() { |
This file contains 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
// Ex. 12.1 | |
//Write a function asHTML which, when given a DOM node, | |
//produces a string representing the HTML text for that node | |
//and its children. You may ignore attributes, just show nodes | |
//as <nodename>. The escapeHTML function from chapter 10 is | |
//available to properly escape the content of text nodes. | |
function escapeHTML(text) { | |
var replacements = {"<": "<", ">": ">", |
This file contains 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
// Ex 13.1 of Chapter 13 in Eloquent Javascript | |
// Write a function called registerEventHandler to wrap the | |
// incompatibilities of these two models. It takes three arguments: first | |
// a DOM node that the handler should be attached to, then the name of | |
// the event type, such as "click" or "keypress", and finally the handler | |
// function. | |
function registerEventHandler (node, eventType, handlerFN) { | |
if (node.attachEvent) { //IE |
OlderNewer