Skip to content

Instantly share code, notes, and snippets.

@oliverswitzer
Created January 20, 2014 21:15
Show Gist options
  • Save oliverswitzer/8529330 to your computer and use it in GitHub Desktop.
Save oliverswitzer/8529330 to your computer and use it in GitHub Desktop.
JS Practice
> typeof(3)
'number'
> typeof(3) === typeof(4.32)
true
> 5/0
Infinity
> 3/"bob"
NaN
> NaN === NaN
false
> typeof(NaN)
'number'
> isNaN(NaN)
true
> Math.pow(2, 3)
8
Describe how the following expression assigns a variable to a value.
> var first_name = "cindy";
--> This will initialize the variable first_name and then assigns the value of "cindy" to that variable.
What is the value of the hello variable in the following expression?
> var hello;
--> undefined
What does the following expression return?
> var y;
> y === "cool";
--> false
What does the following expression return?
> "some" + " person";
--> "some person"
What does the following expression return?
> var first = "Bart";
> var last = "Simpson";
> first + " " + last
--> "Bart Simpson"
What does the following expression return?
> "cool".length;
--> 4
What does the following expression return? Explain the answer.
> "phat" === "phat";
--> true
What does the following expression return?
> typeof("cats");
--> string
What does the following expression return?
> 3 + "bob"
--> "3bob"
Round the number 4.87 to the nearest integer.
--> Math.round(4.87)
Divide the number 6 by the string "2" and explain the result.
--> 3 because even though 2 is a string and 6 is a number, JS allows this by converting the string to a number before dividing.
What does the following expression return? Explain the result.
> 3 * "bob";
--> NaN. JS cant convert bob to a number that you can multiply by 3 so it returns Not A Number (NaN)
Declare the variables x and y.
--> Var x, y
Set the variable hobby to the string "programming".
--> var hobby = "programming"
What does the following expression return?
> var sport;
> sport === undefined
--> true
Demonstrate that "brogrammer" has the type "string".
--> typeof("brogrammer") returns 'string'
_______________________________________________________________________________________________
Part II
Question Click to View Answer
What does the following expression return?
4 > 1;
--> true
What does the following expression return?
"chatty" === "chatty";
--> true because theyre the same type and value
What does the following expression return?
3 <= 300
--> true
Print the string "I just ate a lot" to the console.
--> console.log("I just ate a lot")
if conditions will execute the code in the block if the boolean condition is true. What does the following expression print to the console?
if (5 === 5) { console.log("This code is executed") };
--> "This code is executed"
What does the following expression print to the console?
if (5 > 10) { console.log("Not so sure about this") };
--> It prints nothing to the console, 5 is not less than 10
What does the following expression print to the console?
if (5 > 10) {
console.log("Not so sure about this");
} else {
console.log("walking dead");
}
--> walking dead
What does the following expression print to the console?
if ("candy" === 8) {
console.log("do something with your life");
} else if ("blah" === "blah") {
console.log("just chill, have fun");
} else {
console.log("people are strange");
}
--> "just chill, have fun"
What does the following expression print to the console?
if (5) { console.log("I like peanuts"); }
--> 'I like peanuts'
What doe the following expression print to the console?
if ("") {
console.log("program more");
} else if ("cool") {
console.log("party hard!");
} else {
console.log("blah");
}
--> "party hard!". First if not evaluated because an empty string is false in JS.
What does the following expression return?
!false
--> true
What does the following expression print to the console?
if (!undefined) console.log("This syntax is weird…");
--> "This syntax is weird...". Undefined is falsey.
What does the following code print to the console?
var counter = 0;
while (counter < 3) {
console.log("The counter is at " + counter);
counter++
}
--> The counter is at 0
--> The counter is at 1
--> The counter is at 2
What does the following code print to the console?
var result = 0;
for (var i = 0; i < 5; i++) {
result += i;
}
console.log(result);
--> 10
Arrays are ordered collections of elements. What does the following code print to the console?
var my_array = [1, "bob"];
console.log(my_array[1]);
--> "bob"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment