Created
February 10, 2011 17:01
-
-
Save Melipone/820888 to your computer and use it in GitHub Desktop.
Exercises in Chapter 4 of Eloquent Javascript
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; | |
} | |
range(5); | |
//Ex. 4.3 | |
var mack = []; | |
mack.push("Mack"); | |
mack.push("the"); | |
mack.push("Knife"); | |
show(mack.join(" ")); | |
show(mack.pop()); | |
show(mack); | |
mack.push("bandit"); | |
mack.push("comes to town."); | |
/** | |
js> mack | |
Mack,the,Knife,bandit,comes to town. | |
js> mack.join(" ") | |
Mack the Knife bandit comes to town. | |
js> mack.join(" ").split(" ") | |
Mack,the,Knife,bandit,comes,to,town. | |
*/ | |
//join/split combo do not give back the original array if some of the values originally contained the join string. | |
function startsWith (first,second) { | |
var chunk = first.slice(0,second.length); | |
return chunk == second; | |
} | |
/** | |
js> startsWith("sarah","sa") | |
true | |
js> startsWith("sarah", "sasasasa"); | |
false | |
*/ | |
//Ex. 4.5 | |
function catNames (paragraph) { | |
var lines = paragraph.split("/n"); | |
var catnames = []; | |
for (var i=0;i<lines.length;i++) { | |
var colon = lines[i].indexOf(":"); | |
if (colon >= 0) {// cat names here | |
var names = lines[i].slice(colon+1); | |
var cats = names.split(","); | |
for (var j=0;j<cats.length;j++) | |
catnames.push(cats[j]); | |
} | |
} | |
return catnames; | |
} | |
/** | |
js> catNames(myparr); | |
Doctor Hobbles the 2nd, Noog | |
*/ | |
// unfortunately, I get the space before "Doctor". | |
//Ex. 4.6 | |
function extractDate (sentence) { | |
var colon = sentence.indexOf(":"); | |
var beforeColon = sentence.slice(0,colon); | |
var extract = beforeColon.split(" "); | |
var thedate = extract[1]; | |
var compDate = thedate.split("/"); | |
return new Date(compDate[2],compDate[1],compDate[0]); | |
} | |
var mystr = "died 27/04/2006: Black Leclère"; | |
extractDate(mystr); | |
//Ex 4.7 | |
function between (str, first, second) { | |
var index1 = str.indexOf(first); | |
var index2 = str.indexOf(second,index1); | |
return str.slice(index1+2,index2); | |
} | |
/** | |
js> var mystr = "bu ] boo [ bah ] gzz"; | |
js> | |
js> between (mystr, "[ ", " ]"); | |
bah | |
*/ | |
// looking at the solution, I should have made my solution more general | |
// Ex. 4.8 | |
function formatDate(date) { | |
var myday = date.getDate(); | |
if (myday < 10) | |
myday = "0" + myday; | |
var mymonth = date.getMonth() + 1; | |
if (mymonth < 10) | |
mymonth = "0" + mymonth; | |
return myday + "/" + mymonth + | |
"/" + date.getFullYear(); | |
} | |
// here again, looking at the solution, I feel short of the generalization required in the code | |
// where a pad function within the function formatDate would have taken care of the code duplication. | |
// I'll blame it on too much Java. | |
/** | |
js> print (mydate); | |
Wed Feb 09 2011 16:36:34 GMT-0500 (EST) | |
js> formatDate (mydate); | |
09/02/2011 | |
*/ | |
// Ex. 4.9 | |
// oldest cat | |
// Each cat object consists of property/value pair with birth: as a property | |
function oldestCat (catData) { | |
var oldest = "unknown"; | |
var birthdate = new Date().getTime(); | |
for (var cat in catData) { | |
var catbirth = catData[cat].birth.getTime(); | |
if (catbirth < birthdate) { | |
oldest = cat; | |
birthdate = catbirth; | |
} | |
} | |
return oldest; | |
} | |
// okay, I didn't check to see if the cat was living or not !("death" in cat) | |
// so I got "Spot" as the oldest cat instead of "Bugeye" | |
// Ex. 4.10 | |
// Extended range function to have a start-range number | |
function xRange (start, end) { | |
if (arguments.length < 2) { | |
end = start; | |
start = 0; | |
} | |
if (start > end) { | |
print (end + " not greater than " + start); | |
return; | |
} | |
var arr = []; | |
var k = 0; | |
for (var i=start;i<end;i++,k++) | |
arr[k]=i; | |
return arr; | |
} | |
/** | |
js> xRange(3,8); | |
3,4,5,6,7 | |
js> xRange(8); | |
0,1,2,3,4,5,6,7 | |
*/ | |
// okay, looking at the solutions here, I should have used arr.push(i) to dynamically populate the array. No need for this extra k variable. | |
// Ex. 4.11 | |
function sum (numList) { | |
var sum = 0; | |
for (var i=0;i<numList.length;i++) | |
sum += numList[i]; | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment