Created
November 12, 2012 18:07
-
-
Save leopic/4060892 to your computer and use it in GitHub Desktop.
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
log('== Ex. 1'); | |
// 1) Log to the console only those values that are considered truthy. | |
var q1Var1 = "hello", | |
q1Var2 = 0, | |
q1Var3 = true, | |
q1Var4 = "false", | |
q1Var5 = -1, | |
q1Var6 = undefined, | |
q1Var7 = null, | |
q1Var8; | |
log(q1Var1, q1Var2, q1Var4, q1Var5); | |
log('== Ex. 2'); | |
// 2) Log the contactenation of theses two strings to the console. | |
var q2Var1 = "hello, ", | |
q2Var2 = "is it me you're looking for?"; | |
log(q2Var1 + q2Var2); | |
log('== Ex. 3'); | |
// 3) Coerce the first variable to a number, add the two variables together and log the result to the console. | |
var q3Var1 = "24", | |
q3Var2 = 18; | |
log((parseInt(q3Var1,10)) + q3Var2); | |
log('== Ex. 4'); | |
// 4) Initialize an array and then fill it with 30 values. Each value should be a multiple of 3, starting with 3. Log to the console the 15th value of the array. Log to the console the length of the array. | |
var myArray = []; | |
for (i = 0; i < 30; i++) { | |
myArray[i] = i; | |
} | |
log(myArray.length); | |
log('== Ex. 5'); | |
// 5) Create an object with properties of "foo" and "bar-baz", repectively. The values can be whatever you'd like (be creative!). Log both of these properties to the console. | |
var myObj = { | |
foo: 'fubu', | |
'bar-baz': 'super who?' | |
} | |
log(myObj.foo); | |
log(myObj['bar-baz']); | |
log('== Ex. 6'); | |
// 6) Use the object created in example 5. Augment a new property on the object with the key "qux". The value can be whatever you'd like. Log this new property to the console. | |
myObj.qux = "super dragon!"; | |
console.log(myObj.qux); | |
log('== Ex. 7'); | |
// 7) Use the object from exercise 6. Augment the object with the properties "bar1", "bar2", ... through "bar10". The value of these properties can be whatever you would like. Afterwards print out bar4 to the console. | |
for (i = 0; i < 10; i++) { | |
myObj['bar'+i] = 'prop' + i; | |
} | |
log('== Ex. 8'); | |
// 8) Write a function called forRange that takes parameters Number:from, Number:to, Number:step and Function:iterator, similar to the Array.forEach, this function will iterate over the range from-to in step increments. | |
// simple iterator | |
function forRange (from, to, step, iterator) { | |
//if we only receive 3 arguments | |
if (typeof(arguments[2]) === "function") { | |
//that means that step was skipped | |
iterator = step; | |
step = 1; | |
} | |
//iterate | |
while (from <= to) { | |
iterator.call(this, from); | |
from += step; | |
} | |
} | |
forRange(0, 10, 2, function(i) { | |
log('#', i); | |
}); | |
// The above code should print: # 0 # 2 # 4 # 6 # 8 # 10 | |
log('== Ex. 9'); | |
// 9) Extra credit: make the `step` argument optional and default to 1 if not specified | |
forRange(0, 5, function(i) {log('#', i);}) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment