-
How would you randomly pick an element from an array named myNumbers
?
-
Which of the following is not a Javascript operator?
-
A cookie is set using the below Javascript statement. When will it expire?
document.cookie = "foo=bar;secure";
-
What is Firebug?
-
Which of the following is not a popular Javascript framework?
-
Which of the below can be used to perform an asynchronous request to the web server?
-
What's computed by this code?
(function(n){return (n && n*arguments.callee(n-1)) || 1;})(10);
-
How do you remove a property prop
from an object obj
?
-
Assuming a
refers to a function, how would you execute a
after a delay of one second?
-
What is JSON?
-
The below script has some troubles getting the numbers right. What could be wrong?
var fruitSaladRecipe = "2 apples, papaya, coconut, 5 oranges, apple, banana",
ingredients = fruitSaladRecipe.split(/,\s+/),
ingredientHash = {},
num = 0; // Total number of fruits
for (var i=0 ; i<ingredients.length ; i++) {
var ingredient = ingredients[i],
fruitType = ingredient.match(/(\w+)$/)[1].replace(/s$/, ""),
numPieces;
if (/^\d+/.test(ingredient)) {
var num = ingredient.match(/^(\d+)/)[1];
numPieces = parseInt(num, 10);
}else{
numPieces = 1;
}
if (/^(?:banana)$/.test(fruitType)) {
// Speaking of bananas, let's add some more! Those things are delicious!
numPieces *= 2;
}
num += numPieces;
if (fruitType in ingredientHash) {
numPieces += ingredientHash[fruitType];
}
ingredientHash[fruitType] = numPieces;
}
var message = num + " fruit(s) in total:";
for (var ingredient in ingredientHash) {
if (ingredientHash.hasOwnProperty(ingredient)) {
message += " " + ingredientHash[ingredient] + " " + ingredient + "(s)";
}
}
alert(message);
-
What is the return value of setInterval
?
-
Why doesn't the below code open an alert box with the text "foo"?
function wrapInObject(value) {
return
{
value: value
};
}
alert(wrapInObject("foo").value);
-
Why doesn't the below code open an alert box with the text "foobar"?
var a = "foo",
b = a;
b += "bar";
alert(a);
-
What does the function f
do?
function f(a) {
return function(b) {
return a + b;
};
}
-
Why doesn't this work as intended?
function makeNumberSequenceGenerator(nextValue) {
return function() {
return nextValue++;
};
}
alert(makeNumberSequenceGenerator(10).nextValue().nextValue());
-
Complete the return statement in the function below:
function isFalseBooleanValue(bool) {
// return...
}
-
What's accomplished by using this construct:
(function() {
for (var i=0 ; i<10 ; i++) {
alert(i);
}
})();
instead of just:
for (var i=0 ; i<10 ; i++) {
alert(i);
}
-
The below code is added to an existing website (in an external Javascript file), causing some unexpected effects. What's the problem?
window.onload = function() {
document.write("Hello, world!");
};
-
The below code doesn't work as expected. What could be wrong?
for (var i=0 ; i<10 ; i++) {
var button = document.createElement("button");
button.onclick = function() {
alert("You clicked button number "+i);
};
document.body.appendChild(button).appendChild(document.createTextNode("Button "+i));
}
-
The below code doesn't work as expected. What could be wrong? (Assuming that elements with a class of 'button' receive a suitable styling)
for (var i=0 ; i<10 ; i++) {
document.body.innerHTML += "<div class='button' id='button"+i+"'></div>";
document.getElementById("button"+i).onclick = function() {
alert("You clicked button number "+this.id.replace(/^button/, ""));
};
}
-
How can this function be improved?
function removeLeadingAndTrailingWhitespaceFromString(str) {
return str.replace(/(^\s+)|(\s+$)/ig, "");
}
-
What seems to be the problem with the below code?
function StateMachine(initialState) {
this.state = initialState || 'foo';
return {
transition: function() {
this.state = {foo:'bar', bar:'quux', quux:'foo'}[this.state];
return this;
}
};
}
var sm = new StateMachine();
alert(sm.transition().transition().state);
-
Why doesn't the below code work as expected? (Assuming Javascript 1.6 support)
var isOdd = function(n) {return n&1;},
isDivisibleByThree = function(n) {return !(n%3);},
addOne = function(n) {return n+1;};
alert([1,2,3,4,5,6,7].map(addOne).filter(isOdd && isDivisibleByThree).length);
-
Why doesn't the below code alert "foobar" as expected?
function StringBuffer(initialStr) {
this.append(initialStr);
}
StringBuffer.prototype = {
items: [],
append: function(str) {
this.items[this.items.length] = str instanceof StringBuffer ? str.toString() : str;
return this;
},
toString: function() {
return this.items.join("");
}
};
alert(new StringBuffer("foo").append(new StringBuffer("bar")).toString());
-
Rename
f
,
a
, and
b
to something more suitable:
function f(a, b) {
return function() {
return a.apply(b, arguments);
};
}
-
Why doesn't the below function work as expected?
var isUndefinedOrNull = function(obj) {
return /^(?:undefined|null)$/.test(typeof obj);
};
-
Why doesn't the following function work?
function printZeroToNinetyNine() {
for (var i = 0; i < 100; i += 1) {
setTimeout(function () {
console.log(i);
}, 0);
}
}
-
What is the output of the following function?
function delayedPrint() {
for (var i = 0; i < 100; i += 1) {
(function (value) {
setTimeout(function () {
console.log(i);
}, 1000 * value);
}(i));
}
}
-
What is the output of the following piece of code?
function createObject(name, value) {
var returnObject = {};
returnObject[name] = value;
returnObject['addTo' + name] = function () {
return value += 1;
};
return returnObject;
}
var obj = createObject('Property', 100);
console.log(obj.Property);
console.log(obj.addToProperty());
console.log(obj.Property);
-
Which of the following regular expressions can be used to find all occurrences of two-letter words in a string?
-
Why doesn't the following regular expression work?
function hasANumber(value) {
return /^.*[0-9]*.*$/.test(value);
}
-
What does it mean when a function or feature is referred to as "asynchronous"?
-
How can one ensure that a series of asynchronous calls are invoked in the correct order?
-
Below is a function for executing asynchronous tasks in parallel. Why doesn't it work as expected?
function doInParallel(tasks, cb) {
var numTasks = tasks.length;
tasks.forEach(function (task) {
task();
numTasks -= 1;
if (numTasks === 0) {
cb();
}
});
}
doInParallel([...], function () {
// Execute only when tasks are complete
});
-
What does the `new` operator do when used in conjunction with a function call?
-
Given the below piece of code, what should `obj.value` be set to in order for the if-condition to be true?
var obj = {};
obj.value = ???;
if (obj.value === obj.value.value.value) {
}
-
Given the below code, which of the following is true:
var a = '',
b = '',
c = {value1: a, value2: b},
d = {value2: b, value1: a};
-
Which of the following can set the background color of a DOM element to red?
var element = document.getElementById('my-element');
-
Which of the following is an acceptable way to add a click event handler to a DOM element?
var element = document.getElementById('my-element');
Can you please send answers to above questions to my mail id.
ReplyDeleteCan you please send the answer at yadavanup581@gmail.com
ReplyDeletedude where's the answers?
ReplyDeleteHi, can you please share the answers for the above question.
ReplyDeleteMail id: palak.jain@appsintegra.com