Last active
April 3, 2019 17:04
-
-
Save radicalsauce/60c5544af5a15e69bb7cb745dff5d784 to your computer and use it in GitHub Desktop.
JS Interview Questions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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