Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda