Skip to content

Instantly share code, notes, and snippets.

@elledienne
Created June 26, 2017 21:34
Show Gist options
  • Save elledienne/ed95f6277316b9ef6553f7615ffc1499 to your computer and use it in GitHub Desktop.
Save elledienne/ed95f6277316b9ef6553f7615ffc1499 to your computer and use it in GitHub Desktop.
[INTERVIEW] Coding challenge to test functional programming knowledge and generic JS skills
// Remember to overwite the code in CodeShare, JSFiddle, etc. to restore the inital state of the test and to avoid
// showing to candidates the changes made during other interwies
// TASKS:
// 1. Ask to developer to write a foreach function:
// - The dev should be familiar with .forEach/.each()
// - The dev should be familiar with the interface of these methods
// - The dev should be able to implement it (quite easy, see below for the implementation)
// 2. Ask to developer to write a map function:
// - The dev should be familiar with .map()
// - The dev should be familiar with the interface of this method
// - The dev should be able to implement it, (hard, see below for the implementation)
// JUST REFERENCE, DELETE BEFORE INTERVIEWING
function forEach(collection, iterator) {
// Don't expect them to write this condition, you might ask fot it though
if (!Array.isArray(collection)) {
throw new Error('Collection is not Array');
}
for (let i = 0; i < collection.length; i++) {
iterator(collection[i], i, collection);
}
}
// JUST REFERENCE, DELETE BEFORE INTERVIEWING
function map(collection, iterator) {
const result = [];
forEach(collection, (value, index, collection) => {
result.push(iterator(value, index, collection));
});
return result;
}
@elledienne
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment