Created
January 31, 2011 15:19
-
-
Save Melipone/804169 to your computer and use it in GitHub Desktop.
Exercises in Chapter 2 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
// 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 = ""; | |
var k = 0; | |
while (k++ < 10) { | |
str = str + "#"; | |
print (str); | |
} | |
// Exercise 2-4 | |
var init = 1; | |
for (k = 1;k <= 10;k++) | |
init = init * 2; | |
print (init); | |
var str = ""; | |
for (k = 0;k< 10;k++) { | |
str = str + "#"; | |
print (str); | |
} | |
// Exercise 2-5 | |
var num = prompt("what is 2+2"); | |
if (num == 4) | |
alert ("Good!"); | |
else | |
if (num == 3 || num == 5) | |
alert ("Almost"); | |
else | |
alert ("Imbecile!"); | |
// Exercise 2-6 | |
while (true) { | |
var num = prompt("what is 2+2"); | |
if (num == 4) { | |
alert ("Good!"); | |
break; | |
} | |
else | |
if (num == 3 || num == 5) | |
alert ("Almost"); | |
else | |
alert ("Imbecile!"); | |
} | |
// Guard and default | |
// okay, that's like 2.1 | |
// && acts as a guard which means that if the first part of | |
// the test (on the left of &&) is false, | |
// the second part will not execute | |
js> var test = false; | |
js> test && print ("Executing"); | |
false | |
js> test = true; | |
true | |
js> test && print ("Executing"); | |
Executing | |
// || provides a default if the value to the left of || is "falsy" | |
js> var test = false || 3; | |
js> print (test); | |
3 | |
js> var test = true || 4; | |
js> print (test); | |
true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment