Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
js quiz
/*
* Author: Jan Kassens
*
* Guess the results and run it in Firebug
* If you got all right with the first try you're a JS Samurai (Ninja is overused)
*/
var tasks = {
'try/finally': function(){
var value = 'foo';
var fn = function(change){
try {
return value;
} finally {
if (change){
value = 'bar';
return 'zomg';
}
}
};
console.log(fn());
console.log(fn(true));
console.log(fn());
},
numbers: function(){
console.log(060 + 070 + 080 + 090);
var x = '4', y = '4';
x += 0;
y *= 1;
console.log(x + y);
},
scope: function(){
var scope = 'outer';
(function(){
if (true) var scope = 'inner';
console.log(scope);
})();
scope = 'outer';
(function(){
if (false) var scope = 'inner';
console.log(scope);
})();
scope = 'outer';
(function(){
if (!scope) var scope = 'inner';
console.log(scope);
})();
},
'truthy?': function(){
(function(thing){
console.log(">%s< is ", thing.toString(), thing ? 'truthy' : 'falsy');
return arguments.callee;
})
(0)
(1)
('0')
('1')
(Number(0))
(new Number(0))
(new Number(0) + new Number(0))
('')
(String(''))
(new String(''))
(new String('') + new String(''))
([])
({});
},
prototypes: function(){
try {
var result = Array.prototype.join.call("input", " ");
console.log(result);
} catch (e){
console.log('error');
}
try {
var result = Array.prototype.reverse.call("input", " ").join(" ");
console.log(result);
} catch (e){
console.log('error');
}
},
objects: function(){
var f = function(){
return
{
x: 'one'
};
};
var g = function(){return {x: 'two'};}();
var h = {x: 'three'};
console.log((f() || g || h).x);
}
};
for (task in tasks){
console.group(task);
tasks[task]();
console.groupEnd();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment