>
quote
The function should return the name of top parent when the person has one and null when he does not have a parent at all. I have made the code below but it seems that code prints the name of the person even when he doesnt have a parent instead of printing null :
“` code “
<script type=”text/javascript”>
var people = new Array();
people.push({name: “edd”, id : 1, parentId:null });
people.push({name: “jay”, id : 2, parentId:3});
people.push({name: “elen”, id : 3, parentId: 4});
people.push({name: “john”, id :4, parentId: null});
people.push({name: “bety”, id :5, parentId: null});
function findParent (people, Id){
debugger;
var ind = 0;
for (var i = 0; i < people.length; i++){
if (people[i].id == Id) {
console.log(people[i].id)
if (people[i].parentId != null) {
ind++;
return findParent(people, people[i].parentId);
}
else {
if (ind=0){
return null;
}else {
return people[i].name;
}
}
}
}
return “Id not found”;
}
alert(findParent(people,1));
</script>
</body>
</html>
“` code “