Skip to content

Files

Latest commit

715ef04 · Apr 13, 2014

History

History
This branch is up to date with tcorral/javascript-challenges-book:master.

hoisting1

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Apr 13, 2014

Hoisting and Conditionals Statements

I'm Ernie and my friend is Bert and we wrote this code to tell other people which type of birds we like.

var bird = 'Pidgeons';
( function () {
    if ( typeof bird === 'undefined' ){
        var bird = 'Rubber Duck';
        console.log('Ernie loves his ' + bird );
    } else {
        console.log('Bert loves his ' + bird );
    }
}() );

There should be a problem with our code because for some reason I only see 'Ernie loves his Rubber Duck' when I expected to see 'Bert loves his Pidgeons', could you help me?


Why this is happening?

This is happening because the hoisting problem. Remember that in Javascript there are no block variables.
__match_answer_and_solution__


Please help me and fix this code to get 'Bert loves his Pidgeons'.

var bird = 'Pidgeons';
( function () {
    if ( typeof bird === 'undefined' ){
        var bird = 'Rubber Duck';
        console.log('Ernie loves his ' + bird );
    } else {
        console.log('Bert loves his ' + bird );
    }
}() );
var bird = 'Pidgeons';
( function () {
    if ( typeof bird === 'undefined' ){
        bird = 'Rubber Duck';
        console.log('Ernie loves his ' + bird );
    } else {
        console.log('Bert loves his ' + bird );
    }
}() );
assert(inputs[0] === 'Bert loves his Pidgeons');
var inputs = [];
var backConsole = console;
var console = {
    log: function (str) {
        inputs.push(str);
        backConsole.log(str);
        console = backConsole;
    }
};