I was given the following code and instructions for a technical test for a potential technical role I had applied for:
var input = {
label: 'Earth',
type: 'planet',
children: [{
label: 'Australia',
type: 'country',
children: [{
label: 'Sydney',
type: 'city',
children: [{
label: 'Nuix HQ',
type: 'office',
children: []
}]
}]
}, {
label: 'USA',
type: 'country',
children: [{
label: 'San Franscisco',
type: 'city',
children: [{
label: 'Nuix SF',
type: 'office',
children: []
}]
}, {
label: 'Philadelphia',
type: 'city',
children: [{
label: 'Nuix Philly',
type: 'office',
children: []
}]
}]
}]
};
Task:
Using Javascript please render HTML element that displays above JSON data. It should reflect parent-child relationship by appropriate element nesting and indentation. The structure should be right-justified, meaning the indentation should start from the right and move to the left as the nodes descend. Additionally, when a node is clicked, it should print (to console) all labels on a path from clicked node to the root (Earth).
CSS visual styling is optional (nice to have). You can use any open-source utilities and libraries available in the fiddle, however we also value simplicity of the solution. The solution should be delivered in a fork of this fiddle.
I supplied the additional code as a fork of the original code:
Javascript:
let node = 1;
var result = [];
var arrNodeParent = [];
var arrNodeName = [];
tree = (lvl, parent = 0, node, data) =>
{
let div = `<div class="node lvl${lvl}" node="${node}" parent="${parent==0?0:parent-1}">${data.label}</div>`;
arrNodeParent[node] = parent==0?0:parent-1;
arrNodeName[node] = data.label;
n = node+=1;
(data.children.length)&&data.children.forEach(e=>tree(lvl+1, node, n, e));
result[node]=div;
}
tree(0,0,node,input)
$('.tree').append(...result);
$(".node").click(function(){
complete = false;
currNode = $(this);
while(!complete){
n = +currNode.attr("node");
p = +currNode.attr("parent");
console.log(arrNodeName[n]);
currNode = $("div[node='"+arrNodeParent[n]+"']");
complete = (p==0);
}
});
HTML:
CSS:
.tree {
width: 100%;
}
.node {
font-size: 12px;
padding: 3px;
margin-bottom: 1px;
border: 1px solid black;
background-color: rgb(255, 255, 128);
width: 100px;
margin-left: auto;
margin-right: 0px;
}
.node:hover {
cursor: pointer;
}
.lvl0 {
margin-right: 0;
}
.lvl1 {
margin-right: 20px;
}
.lvl2 {
margin-right: 40px;
}
.lvl3 {
margin-right: 60px;
}
.lvl4 {
margin-right: 80px;
}
.lvl5 {
margin-right: 100px;
}
Now, I was happy with my result, I felt I passed the brief... The client has come back saying I didn't meet the brief... I'm confused here... What specifically have I missed? And is there anything I could have done? I have asked for feedback, but I have yet to receive anything in response... Could someone possibly shed some light?
(data.children.length)&&data.children.forEach(e=>tree(lvl+1, node, n, e));
never seen that before either... thing is, since you still have the perens around your first condition you're actually not saving any bytes.. it's exactly the same size as if you were to have used a conventionalif()
wondering why you did that? \$\endgroup\$