Skip to content

Instantly share code, notes, and snippets.

@radicalsauce
Last active April 3, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radicalsauce/60c5544af5a15e69bb7cb745dff5d784 to your computer and use it in GitHub Desktop.
Save radicalsauce/60c5544af5a15e69bb7cb745dff5d784 to your computer and use it in GitHub Desktop.
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