Skip to content

Instantly share code, notes, and snippets.

@nixsticks
Created January 20, 2014 21:11
Show Gist options
  • Save nixsticks/8529235 to your computer and use it in GitHub Desktop.
Save nixsticks/8529235 to your computer and use it in GitHub Desktop.
What does the following expression return?
4 > 1;
true
What does the following expression return?
"chatty" === "chatty";
true
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") };
Nothing
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!"
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..."
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"
What does the following code print to the console?
var q = "quack";
var num = 555666;
console.log([num, q, true]);
[555666, "quack", true]
What does the following code print to the console?
["uno", "dos", "tres"].length;
3
Create an array with the following elements: "bob", 23, false.
var array = ["bob", 23, false];
Print the last element of my_array to the console.
var my_array = ["horse", "pig", "cow"];
console.log(my_array[2]);
What does the following code print to the console?
var writing_tools = ["pencil", "pen", "marker"];
console.log(writing_tools[writing_tools.length - 1]);
"marker"
What does the following code print to the console?
var b = [1, 2];
b[0] = "master p";
console.log(b);
["master p", 2]
What does the following code print to the console?
var numbers = [2, 4, 6, 8];
var counter = 0;
var sum = 0;
while (counter < numbers.length) {
sum += numbers[counter];
counter++;
}
console.log(sum);
20
What does the following code print to the console?
var words = ["teenage", "mutant", "ninja", "turtles"];
var great_show = "";
for (var i = 0; i < words.length; i++) {
great_show += words[i] + " ";
}
console.log(great_show);
"teenage mutant ninja turtles"
What does the following statement return?
typeof(["cats"]);
"object"
What does the following statement return?
[1, 89] === [1, 89];
false; the two arrays are not the same array
What does the following code return?
var blue = ["da ba dee da ba die"];
blue === blue;
true
Sum all the elements in the following array if they are numbers, otherwise ignore the element. Print the result to the console.
var weird = [1, 40, "bob", [], false, 89];
var sum = 0;
for (var i = 0; i < weird.length; i++) {
if (typeof i === "number") {
sum += i;
}
}
console.log(sum);
Update the "shots" element in the following array to be "don't do it".
var beverages = ["beer", "wine", "shots"];
beverages[beverages.indexOf("shots")] = "don't do it";
(or, if you want, beverages[2] = "don't do it";)
What are the falsey values in JavaScript? What does it mean when a value is falsey?
false; null; undefined; ""; 0. Falsey evaluates to false even though it is not actually the value false.
Concatenate all the elements in the following array that begin with the letter "b" and print the resulting sentence to the console.
var heaven = [34, [], "bodacious", "barbecues", "begin", "whatever", "by", "battering", true, "bacon"];
var sentence = "";
for (var i = 0, word; word = heaven[i]; i++) {
if (word[0] == "b") {
sentence += word[0] + " ";
}
}
console.log(sentence);
(I know you could also do this by making the conditional i < heaven.length but I like it better this way)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment