Skip to content

Instantly share code, notes, and snippets.

@jakeklassen
Last active August 29, 2015 14:16
Show Gist options
  • Save jakeklassen/38ea97f6c1d2bf219def to your computer and use it in GitHub Desktop.
Save jakeklassen/38ea97f6c1d2bf219def to your computer and use it in GitHub Desktop.
Some JavaScript questions.
/* jshint ignore:start */
// Complete the questions below and save the file as first-last.js
// Question 1.
// Looking at the loop below, we expect the output to be 0 through 4. However,
// the loop below will output the value "5" five times. Fix it.
for (var i = 0; i < 5; ++i) {
setTimeout(function () {
console.log(i);
}, i * 500);
}
//------------------------------------------------------------------------------
// Question 2.
// What is the result of envoking the function log? Why?
log('Hello!');
var log = function (output) {
console.log(output);
}
//------------------------------------------------------------------------------
// Question 3.
(function () {
"use strict";
// Is the assignment below valid in this functions scope?
// Explain.
name = 'Fuzzy Matter';
});
//------------------------------------------------------------------------------
// Question 4.
function op(operator, num1, num2) {
switch (operator) {
case '*':
return num1 * num2;
case '/':
return num1 / num2;
case '-':
return num1 - num2;
case '+':
return num1 + num2;
default:
console.warn('unknown operator');
return undefined;
}
}
// Given the function op(), use function binding to create a function
// multiply() that automatically passes the string "*" and gives the correct
// result.
// For example, console.log(multiply(2,2)); should output 4.
//------------------------------------------------------------------------------
// Question 5.
// Which of the 2 function statements is affected by hoisting?
var fn1 = function () { };
function fn2 () { }
//------------------------------------------------------------------------------
// Question 6.
var data = [1, 2, 3, 4];
// Why is using delete with arrays bad practice?
// What is the length of the data array at this point?
delete data[1];
//------------------------------------------------------------------------------
// Question 7.
function Person (first, last) {
this.first = first || '';
this.last = last || '';
}
// Given the Person constructor above, create a method name() that simply
// returns first + ' ' + last, but is shared by all instances of Person.
//------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment