Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Forked from say2joe/JS-Quiz-Solutions.js
Created October 27, 2016 18:21
Show Gist options
  • Save crazy4groovy/9c16e7f48856917268ad8d7947a81759 to your computer and use it in GitHub Desktop.
Save crazy4groovy/9c16e7f48856917268ad8d7947a81759 to your computer and use it in GitHub Desktop.
My solutions to former employer's JS quiz for new employees
// Please save all your work in https://jsfiddle.net/
// Return the link to your work
// Answer five out of the seven questions
// Use test data when provided, however your functions should work with any similiarly structured data
// Use any additional libraries you like
// 1: Refactor this function to make it scalable.
var goto = function(evt, where, project, scenario, item, id){
if (evt && evt.stopPropagation){ evt.stopPropagation(); }
switch(where){
case "scenarioEdit": GotoService.scenarioEdit(project); break;
case "dashboard": GotoService.dashboard(project, scenario, item, id); break;
case "projects": GotoService.projects(project, scenario); break;
case "base": GotoService.base(project, scenario, item, id); break;
case "scenario": GotoService.scenario(project, scenario, item); break;
case "calculate": GotoService.calculate(project, scenario); break;
case "simulate": GotoService.simulate(); break;
default: GotoService.base(project, scenario, item, id); break;
}
};
/* Answer 1:
var goto = function(evt, where, project, scenario, item, id) {
if (!arguments.length) return false;
if (evt.stopPropagation) evt.stopPropagation();
GotoService[where].apply(evt, [].slice.call(arguments, 2));
};
*/
// 2: Write a function that searches collection "projects" to find the object with the lowest "e" value and return that object's "i" value.
var projects = [{i:1, e:41}, {i:5, e:4}, {i:3, e:2}, {i:1, e:90}, {i:5, e:40}, {i:3, e:20}];
/* Answer 2:
Array.prototype.lowest = function(prop, val) {
var idx = 0, lowest = this[idx][prop];
if (lowest === undefined) return null;
this.forEach(function(obj, i, a) {
if (!i) return; // Skip the first iteration.
if (typeof obj === 'object' && obj[prop] && obj[prop] < lowest) {
lowest = obj[prop]; idx = i;
}
});
return val ? this[idx][val] : this[idx];
};
projects.lowest('e','i');
*/
// 3: write a function that sorts collection "scenarios" by "lastModifiedOn" in descending order (most current first) and outputs the results.
var scenarios = [
{
"id": 1,
"uuid": "b47be837de14692f22298ad0d8acf5a5",
"name": "MASTER PROJECT",
"description": "MASTER PROJECT",
"isMaster": true,
"auditInfo": {
"createdOn": "2014-03-09T09:33:41.973Z",
"createdBy": {
"uuid": "system",
"name": "me"
},
"lastUpdatedOn": "2014-03-09T09:33:41.973Z",
"lastUpdatedBy": {
"uuid": "system",
"name": "me"
}
}
},
{
"id": 2,
"uuid": "9806c3d1fbe236f4be52055f24c9623a",
"name": "Test calculation Sandesh",
"isMaster": false,
"auditInfo": {
"createdOn": "2011-03-10T08:55:22.280Z",
"createdBy": {
"uuid": "UUID-1",
"name": "me"
},
"lastUpdatedOn": "2011-03-10T08:55:22.280Z",
"lastUpdatedBy": {
"uuid": "UUID-1",
"name": "me"
}
}
},
{
"id": 4,
"uuid": "fbde082a08923612a4a70a73f7a563e5",
"name": "My Renamed Project - 1426026460724",
"description": "This is my new description - 1426026460750",
"isMaster": false,
"auditInfo": {
"createdOn": "2015-02-10T22:34:48.166Z",
"createdBy": {
"uuid": "UUID-1",
"name": "me"
},
"lastUpdatedOn": "2015-02-10T22:35:30.033Z",
"lastUpdatedBy": {
"uuid": "UUID-1",
"name": "me"
}
}
},
{
"id": 3,
"uuid": "117d4a091d2b3125bc43d18ae20d6962",
"name": "Chengchang Scenario Calculation Test",
"description": "my new description",
"isMaster": false,
"auditInfo": {
"createdOn": "2014-03-10T16:59:24.334Z",
"createdBy": {
"uuid": "UUID-1",
"name": "me"
},
"lastUpdatedOn": "2014-03-10T23:58:46.575Z",
"lastUpdatedBy": {
"uuid": "UUID-1",
"name": "me"
}
}
},
{
"id": 5,
"uuid": "2ccd9efe05623c84b358fea1684832f1",
"name": "My Renamed Project - 1426117320056",
"description": "This is my new description - 1426117320078",
"isMaster": false,
"auditInfo": {
"createdOn": "2015-01-11T23:49:14.161Z",
"createdBy": {
"uuid": "UUID-1",
"name": "me"
},
"lastUpdatedOn": "2015-01-11T23:49:57.248Z",
"lastUpdatedBy": {
"uuid": "UUID-1",
"name": "me"
}
}
},
{
"id": 6,
"uuid": "bb48e52a0cbd36869a6364152a8e9fc4",
"name": "My Renamed Project - 1426118490264",
"description": "This is my new description - 1426118490279",
"isMaster": false,
"auditInfo": {
"createdOn": "2014-04-12T00:08:30.789Z",
"createdBy": {
"uuid": "UUID-1",
"name": "me"
},
"lastUpdatedOn": "2014-04-12T00:09:12.766Z",
"lastUpdatedBy": {
"uuid": "UUID-1",
"name": "me"
}
}
},
{
"id": 7,
"uuid": "2891aa64223532aabff05a8c4ec76d6c",
"name": "My Renamed Project - 1426119218963",
"description": "This is my new description - 1426119218980",
"isMaster": false,
"auditInfo": {
"createdOn": "2015-03-12T00:20:41.768Z",
"createdBy": {
"uuid": "UUID-1",
"name": "me"
},
"lastUpdatedOn": "2015-03-12T00:21:25.463Z",
"lastUpdatedBy": {
"uuid": "UUID-1",
"name": "me"
}
}
}];
/* Answer 5:
scenarios.sort(function(a, b){
return Date.parse(a.auditInfo.lastUpdatedOn) < Date.parse(b.auditInfo.lastUpdatedOn);
});
*/
// 4: write a function which takes an array of words and a string and finds all occurances of each array word in the string and returns that data as a collection.
// findWords(["cat", "at"], "Of the many cats in the catacombs, only the black cat and the white cat where at bat") // returns [{"cat": 2}, {"at": 1}]
// findWords(["foo", "bar", "a"], "after a foo, bar and foobar, we all went to a bar ") // returns [{"foo": 1}, {"bar": 2}, {"a":2}]
/* Answer 4:
function findWords(terms, phrase) {
var arrResults = [];
terms.forEach(function(term){
var result = {}, re = new RegExp('\\b'+ term + '\\b','g');
var matches = phrase.match(re) || [];
result[term] = matches.length;
arrResults.push(result);
});
return arrResults;
}
findWords(["foo", "bar", "a"], "after a foo, bar and foobar, we all went to a bar ");
*/
// 5: Write a function that sums all the values of "data".
var data = [[[1,2,3,4,5],[[1],[2],[3],[4],[5],[6],[[1]]],[9,8,7,6,5,[1,2,3]]],[10,20,30,40,50],[21,33,55,66,77,88],[11,12,13,14,15,16,17,18,19],[[1000,1234],154,2122],[45,66,88,99,100,101]];
/* Answer 5:
Array.prototype.sum = function() {
var sum = 0, values = this.join();
return values.split(',').reduce(function(pre, cur){
return pre + (isNaN(cur)? 0 : parseInt(cur,10));
}, 0);
}
data.sum();
*/
// 6: write function "makeObject" which takes two strings as parameters and returns an object. The first parameter will be the nested object made, and the second parameter will be the value of that object.
// makeObject("foo.bar", "buzz") // returns {foo: {bar: "buzz"}}
// makeObject("one.two.three", "foobar") // returns {one: {two: {three: "foobar"}}}
// makeObject("kingdom.phylum.class.order", "primates") // returns {kingdom: {phylum: {class: {order: "primates"}}}}
/* Answer 6:
function makeObject(ns, value) {
var obj = {};
function addProperty(name, i) {
var pObj = {};
if (!i) pObj[name] = value;
else pObj[name] = obj;
obj = pObj;
}
ns = ns.split('.').reverse();
ns.forEach(addProperty);
return obj;
}
makeObject("kingdom.phylum.class.order", "primates");
*/
// 7: when sorting an array of names and acronyms, all of the acronyms are listed at the top. Please make it so sorting an array puts all items in the correct alphabetal order
var data = [
'ACL',
'accelerated graphics port',
'ATM',
'Address Resolution Protocol',
'American Standard Code For Information Interchange',
'Active Server Page',
'ASP'
];
console.info(data.sort()); //["ACL", "ASP", "ATM", "Active Server Page", "Address Resolution Protocol", "American Standard Code For Information Interchange", "accelerated graphics port"]
//What we want the return to be is ["ACL", "accelerated graphics port", "Address Resolution Protocol", "American Standard Code For Information Interchange", "Active Server Page", "ATM", "ASP"]
/* Answer 7:
data.sort(function(a, b) {
return a.toLowerCase() > b.toLowerCase();
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment