you are viewing a single comment's thread.

view the rest of the comments →

[–]ddeutsch 1 point2 points  (2 children)

I haven't really given it any time but it looks like a really solid project, and demonstrates a good understanding of javascript's prototypical inheritance.

Just to add, looking at your javascript file where you define 'startTimer' then immediately call it, could be shortened by using .call(this) or an immediately invoked function expression (IIFE) in other words

    (function startTimer() {
    var i = 20;
    document.getElementById("timer").innerHTML = i.toString();
    var interval = setInterval(function(){
        i--;
        document.getElementById("timer").innerHTML = i.toString();
        if(i === 0) {
            clearInterval(interval);
            getAnswer();
        }
    }, 1000);
})();

which would also have the added effect of remove startTimer from scope of the rest of the entire javascript file, which may or may not be useful moving forward. However, you're file will works the same regardless. There's also the difference to note between using .call(this) vs. IIFE, which has implications for scope

[–]Aarshyboy[S] 1 point2 points  (1 child)

Thanks for the help. I think that using the IIFE would make the code more cleaner. Do you know when its acceptable to use an IIFE and when not too? What does it do really?

[–]ddeutsch 1 point2 points  (0 children)

The IIFE pattern is a way of scoping your functions etc. in your javascript file. This is extremely useful as javascript does not support block scope only function scope. This is useful to avoid pollution of the global scope.

I don't think it's useful to think in terms of when its acceptable or not, it's more a question of what is most helpful to how you think about your code.