Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS Interview Questions
// PROMPT ONE:
var getUserComments = function(callback) {
// pretend this performs some async operation like an Ajax call
var commentCount = 50;
callback({ comments: commentCount });
};
var User = {
fullName: 'Joe Shmoe',
printUserInfo: function() {
getUserComments(function(data) {
console.log(this.fullName + ' made ' + data.comments + ' comments');
});
}
};
User.printUserInfo();
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// PROMPT TWO:
var theFinalCountDown = [1, 2, 3, 4, 5];
for (var i = 0; i < theFinalCountDown.length; i++) {
var count = theFinalCountDown[i];
setTimeout(function() { console.log(count); }, 1000);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// PROMPT THREE (Non-React):
console.log("Hi!")
setTimeout(function() {
console.log('Hello!')
}, 0)
console.log("Hola!")
// In what order do these return?
// What is going on here?
// PROMPT THREE (React):
this.setState({ canView: true }, () => { console.log("Hello!") })
// In what order would these operations happen?
// Why would you want to set a callback in the setState instead of running the console log immediately afterwards?
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// PROMPT FOUR:
function runMultipleQueries(queries) {
var results = [];
queries.forEach(doQuery);
return results;
function doQuery(query) {
findData(query)
.then(results.push.bind(results));
}
}
console.log(runMultipleQueries([queryA, queryB, queryC]))
// Assume findData exists and returns a promise and each of these query variables are defined. What would this output if you ran the last line?
// How would you get this to log all the results in sequence (in the order they were passed, queryA, queryB and so on)?
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment