Skip to content

Instantly share code, notes, and snippets.

@oliverswitzer
Created January 21, 2014 20:18
Show Gist options
  • Save oliverswitzer/8547570 to your computer and use it in GitHub Desktop.
Save oliverswitzer/8547570 to your computer and use it in GitHub Desktop.
What does the following code evaluate to?
var first_name = function (name) {
return name;
}
first_name("bob");
--> 'bob'
What does the following code evaluate to?
function add(x, y) {
return x + y;
}
add(2, 3);
--> 5
Create an anonymous function and assign it to the variable full_name. The function should take two arguments, first_name and last_name, and return the two strings concatenated.
-->
function Concat(arg1, arg2) {
return arg1 + " " + arg2
};
What does the following code print to the console?
function square (x) {
return(x * x);
}
console.log(square(5));
--> 25
What does the following code print to the console?
function square (x) { return(x * x) };
function cube (x) {
return(x * square(x));
}
console.log(cube(2));
--> 8
Invoke the my_fun() function.
function my_fun() { return "I am having fun" };
-->
my_fun()
What does the following code print to the console?
var what = function () { return "HI!!!!" };
console.log(typeof(what));
--> function
What does the following code print to the console?
var max_value = function(array) {
var result = array[0];
for (var i = 0; i < array.length; i++) {
if (array[i] > result) {
result = array[i];
};
}
return result;
}
console.log(max_value([1, 50, 2]));
--> 50 (it will return the maximum value of an array)
What does the following code print to the console?
var arr = ["boy", 42, 23, function () { return "gotta catch 'em all, Pokemon!" }, 56];
console.log(arr[3]());
--> gotta catch em all, Pokemon!
What is the code to invoke the following function?
function kesha() {
return "Your love is my drug";
}
-->kesha()
What does the following code print to the console?
var lambchop = function () {
return "This is the song that never ends";
}
console.log(lambchop());
--> This is the song that never ends
What does the following code print to the console?
function doctor_name (last_name) {
return "Dr. " + last_name
}
console.log(doctor_name());
--> Dr. undefined
What does the following code print to the console?
function dwelling(name) {
if (typeof(name) != "string") { throw "ArgumentError" };
return name + " cave";
}
console.log(dwelling(42));
--> ArgumentError
What does the following code print to the console?
function add(x, y) {
return(x + y);
}
console.log(add(1, 2, 3, 4, 10, 20));
--> 3 (only the first two arguments that are passed into the function are evaluated)
What does the following function return?
function args () {
return arguments;
}
console.log(args(8, 7, 6, 5, 4));
--> {'0':8, '1':7, '2':6, '3':5, '4':4}
What does the following code print to the console?
function lamp() {
var my_special_variable = "I am special";
}
console.log(my_special_variable);
--> my_special_variable is not defined (var makes it local and only available within the function)
What does the following code print to the console?
function book() {
good_book = "Slaughterhouse Five";
}
book()
console.log(good_book);
--> "Slaughterhouse Five"
num = 23;
function evil () {
num += 5;
}
evil();
console.log(num);
--> 28
Create a function called min_value() that accepts an array of numbers as an argument and returns the smallest value from the array.
-->
function minValue(arr) {
returnVal = arr[0];
for(i = 0; i < arr.length; i++) {
if(arr[i] < returnVal) {
returnVal = arr[i];
}
}
return returnVal;
}
Invoke the song() function.
function song () {
return "Mary had a little lamb";
}
--> song()
Define a function called add_numbers() that adds two numbers and throws an error if the arguments supplied to the function are not numbers.
-->
function addNumbers(num1, num2) {
if(typeof(num1) != 'number' || typeof(num2) != 'number') {
throw ArgumentError "poop"
} else {
return num1 + num2;
}
}
Invoke the bad_hairday() function in the following array.
arr = [0, "nice", function bad_hairday () { return "YOLO" }];
--> arr[2]()
_________________________________________________________________________ GOT UP TO HERE
Define an anonymous function and assign it to the your_name variable. The function should return "Snoop Dogg".
What does the following code print to the console?
var result;
if (undefined) {
result = function () { return "blah blah blah"; }();
} else {
result = function () { return "meow meow meow"; }();
}
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment