Skip to content

Instantly share code, notes, and snippets.

@Melipone
Melipone / Chapter 2
Created January 31, 2011 15:19
Exercises in Chapter 2 of Eloquent Javascript
// 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 = "";
@Melipone
Melipone / Chapter 3
Created January 31, 2011 15:21
Exercises in Chapter 3 of Eloquent Javascript
// Exercise 3.1
function absolute (num) {
if (num >= 0)
return num;
return (- num);
}
// I realize that testing the other branch first is more efficient
@Melipone
Melipone / Chapter4.js
Created February 10, 2011 17:01
Exercises in Chapter 4 of Eloquent Javascript
//Ex. 4.2
function range (num) {
var arr = [];
if (isNaN(Number(num)))
alert ("Invalid number");
else
for (var i=0;i&lt;num;i++)
arr[i]=i;
return arr;
@Melipone
Melipone / reflection.js
Created February 15, 2011 16:01
Javascript reflection
var test = function () {
function first () {
print ("first");
}
function second () {
print ("second");
}
};
// add a function to Object.prototype
Object.prototype.getOwnMethods = function(){
@Melipone
Melipone / Chapter6.js
Created February 16, 2011 02:38
Exercises in Chapter 6 of Eloquent Javascript
// 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) {
@Melipone
Melipone / dom1.html
Created February 20, 2011 04:03
Week 4 homework #1 on the theory of the DOM
<script type="text/javascript">
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function countTextNodes() {
@Melipone
Melipone / dom2.html
Created February 20, 2011 04:06
Week 4 homework #2 on the Theory of the DOM
<script type="text/javascript">
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function countTextNodes() {
@Melipone
Melipone / dom3.html
Created February 20, 2011 04:08
Week 4 homework #3 on the Theory of the DOM
<script type="text/javascript">
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function countTextNodes() {
@Melipone
Melipone / Chapter12.js
Created February 28, 2011 06:06
Exercises from Chapter 12 of Eloquent Javascript
// 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 = {"<": "&lt;", ">": "&gt;",
@Melipone
Melipone / Chapter13.js
Created February 28, 2011 06:09
Exercises from Chapter 13 of Eloquent Javascript
// 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