Javascript Exercises solution:
1. What is the difference between the following 2 statements?
setTimeout(booyah, 2000);
setTimeout(booyah(), 2000);
Answer:
In this code snippet, first line will invoke booyah function after 2000 milliseconds.
Second line of code with booyah() inside setTimeout means this function booyah() will be
called immediately.
2. What do the following 2 alerts display (answer without running the code)?
var myfunc = function(a, x) {
return a * x;
};
var x = myfunc(2, 3);
var y = myfunc;
alert(x);
alert(y(2,3));
Answer:
Both the alert will display 6 as pop up in the browser which comes from 2 * 3 input fields.
So 6 will be displayed / popped up twice.
3. Write functions booyah1 and booyah2 so that in both cases below, an alert box comes up after 2 seconds that
says “BOOYAH!”
setTimeout(booyah1, 2000);
setTimeout(booyah2(), 2000);
Answer:
function booyah1() {
alert("BOOYAH!");
}
function booyah2() {
return function() {
alert("BOOYAH!");
};
}
setTimeout(booyah1, 2000);
setTimeout(booyah2(), 2000);
4. What is "Unobtrusive Javascript"? What is the practical application of Unobtrusive Javascript (and the reasons
for using it)?
Answer:
Unobtrusive JavaScript is a way of writing JavaScript so that your site visitors are not
shut out of your site for one of these reasons—even if your JavaScript is not working
correctly for them, they should still be able to use your site, albeit at a more basic level.
One of the practical application of using Unobtrusive JavaScript is separating the
javascript code from the html code by writing all javascript code inside .js file. The main reasons for using this
is code will be less error prone, debugging gets easier, easy to update the code. The idea is that
javascript code should be means to enhance our web page rather than absolute neccessary elements
to make the website.