Created
February 9, 2017 09:26
-
-
Save ilagnev/278df4d00ae8e9533df93ac2fa7e4ce7 to your computer and use it in GitHub Desktop.
it was test task for the job to the position of junior node js developer
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
<!doctype html> | |
<html> | |
<head></head> | |
<body> | |
<h1>Interview test</h1> | |
<p>You have to implement missing part of the application (`Parallel`) that making the code to be compiled and executed without exceptions and assertions in browser console.</p> | |
<script> | |
/* | |
* You have to implement missing part of the application that making code below (which untouchble) | |
* to be compiled and executed without exceptions and assertions. | |
*/ | |
function Parallel() { | |
// YOUR CODE IS HERE | |
// default settings | |
var config = { | |
parallelJobs: 1// simultaneously running jobs | |
}; | |
// overwrite default settings | |
if (arguments.length && typeof(arguments[0]) == 'object') { | |
for (i in arguments[0]) { | |
config[i] = arguments[0][i]; | |
} | |
} | |
return { | |
queue: [],// jobs queue | |
queueProcessed: [],// use to keep jobs results order | |
runTasks: 0,// counter | |
doneCallback: undefined, | |
// add jobs to queue | |
job: function (job) { | |
// add inner task obj to queue | |
this.queue.push({ | |
job: job, | |
result: undefined | |
}); | |
this.runJob();// run next job | |
return this;// pipes rules \m/ | |
}, | |
// perform job | |
runJob: function() { | |
var self = this;// keep context in closure | |
// check max simultaneously runned jobs | |
if (this.runTasks >= config.parallelJobs) return; | |
// get next task and perform its job | |
var task = this.queue.shift() | |
task.job(function(result) { | |
// save job result and run afterjob checks | |
task.result = result; | |
self.taskCompleted.call(self); | |
}); | |
// move task obj to processed storage | |
this.queueProcessed.push(task); | |
this.runTasks++; | |
}, | |
// check conditions when job is completed | |
taskCompleted: function() { | |
this.runTasks--; | |
// if it was last task in queue then call done func | |
if (!this.queue.length && !this.runTasks) { | |
// assemble jobs results to array and execute done func | |
if (typeof this.doneCallback === 'function') { | |
this.doneCallback(this.queueProcessed.map(function(j) { | |
return j.result; | |
})); | |
} | |
} else if (this.queue.length) { | |
this.runJob();// run next job | |
} | |
}, | |
// save final callback | |
done: function(callback) { | |
this.doneCallback = callback; | |
} | |
} | |
// YOUR CODE IS HERE | |
} | |
</script> | |
<script> | |
/************************************************ | |
* Please don`t change the code bellow this line * | |
************************************************/ | |
var runner = new Parallel({ | |
parallelJobs: 2 // Important! Max number of jobs which should be in queue simultaneously | |
}); | |
runner | |
.job(step1) | |
.job(step2) | |
.job(step3) | |
.job(step4) | |
.done(onDone); | |
function step1(done) { | |
console.log('step1'); | |
setTimeout(done, 1000, 'hello world'); | |
} | |
function step2(done) { | |
console.log('step2'); | |
setTimeout(done, 1200, 'Job succeded'); | |
} | |
function step3(done) { | |
console.log('step3'); | |
setTimeout(done, 1500, 'step3'); | |
} | |
function step4(done) { | |
console.log('step4'); | |
setTimeout(done, 100, 'step4'); | |
} | |
var isPassed = false; | |
function onDone(results) { | |
console.log('onDone', results); | |
console.assert(Array.isArray(results), 'expect result to be array'); | |
console.assert(results[0] === 'hello world', 'Wrong answer 1'); | |
console.assert(results[1] === 'Job succeded', 'Wrong answer 2'); | |
console.assert(results[2] === 'step3', 'Wrong answer 3'); | |
console.assert(results[3] === 'step4', 'Wrong answer 4'); | |
console.log('Thanks, all works fine'); | |
isPassed = true; | |
} | |
setTimeout(function(){ | |
if(isPassed) return; | |
console.error('Test is not done.'); | |
}, 3500); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment