Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
TestDome Challenge Closest Relative

I'm tried to solve this challenge from testdome and i got stuck. The following HTML represents a family tree:

<James>
  <Dave></Dave>
  <Mike></Mike>
  <Sarah></Sarah>
</James> 

Implement the closestRelative function so that when a parent HTML element is passed, the function returns the parent's closest relative whose name matches the relativeName, and obeys the following rules:

The parent parameter is the HTML element of which the closest relative will be a descendant. Each member of the family, including children, may also be a parent to one or more children. Children are more closely related to the parent than grandchildren. If several children in the same generation have the same name, then the first child in the tree is considered the closer relative. If no matching relative is found the function should return null. For example, the below code should print 'MIKE' for the given family tree:

let parent = document.getElementsByTagName('James')[0];
let relative = closestRelative(parent, 'Mike');
console.log(relative && relative.tagName); // prints MIKE

I googled for answer and i find only jquery method for solving this problem. I dont know Jquery so i'm trying to solve it with js. This is my code :

function closestRelative(parent, relativeName) {
  result = parent.find(relativeName);
  if (result.length === 1 ){
    return result
  } else if (result.length > 1) {
      let lowest = 0 ;
      let lowestIdx = 0;
      result.forEach( function (idx, item) {
        if(idx === 0 ){
          lowest = item.closest().index(parent);
        } else {
          if (item.closest().index(parent) < lowest) {
            lowestIdx = idx;
            lowest = item.closest().index(parent);
          }
        }
      });
    return ([result[lowestidx]]);
  }
  else {
    return ([]);
  
} 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I found the right answer on this site ! https://khw1031.github.io/posts/closest-relative-testdome/

function closestRelative(parent, relativeName) {
  const queue = [...parent.children];
  const tagName = relativeName.toUpperCase();
  let el;
  
  while (queue.length > 0 ){
    el = queue.shift();
    if (el.tagName === tagName) return el;
    if(!el.hasChildNodes()) {continue ;}
    
    for (const childEl of el.children) {
      queue.push(childEl)
    }
  }
  return null
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!