Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend Skills Test Submission - Duncan Prince #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Ontraport_submission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
function toAssociativeArray(currentObj, path, finalObj) {
//This function pertains to question 1.
//Takes in multidimensional object 'currentObj'.
//Initially, path = '' and finalObj = {}.
//After completion, finalObj will be the desired associative array.

if (typeof(currentObj) !== "object") {
finalObj[path] = currentObj;
}
//recursively call on each key w/ corresponding path
for (let key in currentObj) {
toAssociativeArray(currentObj[key], path + '/' + key, finalObj);
}
}

function fromAssociativeArray(currentObj, finalObj) {
//This function pertains to question 2.
//Takes in associative array currentObj.
//Initially, finalObj = {}.
//After completion, finalObj will be the desired multi-dimensional object.

let newObj = {};
let i = 0;
for (let key in currentObj){
//parse the path for easier handling
newObj[i] = JSON.parse(JSON.stringify(key)).split("/");

//create reference for use in next for loop
var finalCopy = finalObj;

for(var j = 1; j < (newObj[i]).length; j++){

if (typeof(finalCopy[newObj[i][j]]) == 'undefined'){
finalCopy[newObj[i][j]] = {};
}
else{
//do nothing (key/directory already exists)
}
if (j < (newObj[i]).length-1){
finalCopy = finalCopy[newObj[i][j]];
}
else{
finalCopy[newObj[i][j]] = currentObj[key];
}
}
i++;
}
}

//Object used in testing
let nestedObj = {
'one':
{
'four': [5, 6, 7],
'two': 3
},
'eight':
{
'nine':
{
'ten': 11
}
}
};
let path = '';
let finalObj1 = {};

//test first function
toAssociativeArray(nestedObj, path, finalObj1);
console.dir(finalObj1);

//copy result, for input to second function
let newObj = JSON.parse(JSON.stringify(finalObj1));
let finalObj2 = {};

//test second function
fromAssociativeArray(newObj, finalObj2);
console.dir(finalObj2);

alert("Success");