Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
How to compare two objects using JavaScript language and return a number between 0 and 100 representing the percentage of similarity as the result

I followed this tutorial (https://www.tutorialspoint.com/compare-two-objects-in-javascript-and-return-a-number-between-0-and-100-representing-the-percentage-of-similarity) to be able to compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity.

However, that only works for exact key-value pairs in both objects. I also need to be able to compare number intervals (like on the third "if" statement below for example). The expected result is 70, but currently I am getting 80 (if you comment all "if" statements after the second one, it will give you 40 as the result, which is Correct).

My problem starts when I write the third "if" statement and continue with the others. Actually, after the 3rd "if" statement, the other "if's" don't make any difference on the final result (I don't know why). Could somebody help to find a suitable solution, please?

const a = {
  especie: 'cachorro',
  idade_min: 1,
  idade_max: 3,
  sexo: 'M',
  peso_min: 5,
  peso_max: 10,
  tamanho: 50,
  porte: 100,
  cor: 'preta',
  raça: 'pitbull',
  castrado: true,
  vacinado: true,
};

const b = {
  especie: 'cachorro',
  idade: 2,
  sexo: 'M',
  peso: 10,
  tamanho: 25,
  porte: 100,
  cor: 'branca',
  raça: 'pitbull',
  castrado: false,
  vacinado: false,
};

const findMatch = (first, second) => {
  const firstLength = Object.keys(first).length;
  const secondLength = Object.keys(second).length;
  const smaller = firstLength < secondLength ? first : second;
  const greater = smaller === first ? second : first;

  const count = Object.keys(smaller).reduce((acc, key) => {
    let counter = acc;

    if (Object.keys(greater).includes(key)) {
      if (greater[key] === smaller[key]) {
        console.log(counter);
        return ++counter;
      };

      if (b.idade <= a.idade_max && b.idade >= a.idade_min) {
        return ++counter;
      };

      if (b.peso <= a.peso_max && b.peso >= a.peso_min) {
        return ++counter;
      };

      if (b.tamanho <= a.tamanho) {
        return ++counter;
      };

      if (b.porte <= a.porte) {
        return ++counter;
      };
    };

    console.log(counter);
    return counter;
  }, 0);

  return (count / Math.min(firstLength, secondLength)) * 100;
};

console.log(findMatch(a, b)); // expected result = 70?
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

your function isn't dynamic , it use constant object values and keys from global scope anyway check this it will work fine i think

const a = {
    especie: 'cachorro',
    idade_min: 1,
    idade_max: 3,
    sexo: 'M',
    peso_min: 5,
    peso_max: 10,
    tamanho: 50,
    porte: 100,
    cor: 'preta',
    raça: 'pitbull',
    castrado: true,
    vacinado: true,
};

const b = {
    especie: 'cachorro',
    idade: 2,
    sexo: 'M',
    peso: 10,
    tamanho: 25,
    porte: 100,
    cor: 'branca',
    raça: 'pitbull',
    castrado: false,
    vacinado: false,
};

const findMatch = (first, second) => {
    const firstLength = Object.keys(first).length;
    const secondLength = Object.keys(second).length;
    const smaller = firstLength < secondLength ? first : second;
    const greater = smaller === first ? second : first;
    const smallObjKeys = Object.keys(smaller)

    const count = smallObjKeys.reduce((counter, key) => {
        // this to check the similarity between normal keys 
        if (greater[key] === smaller[key])
            return ++counter;

        // this to check the similarity between keys have range to compare with 
        if (smaller[key] <= greater[`${key}_max`] && smaller[key] >= greater[`${key}_min`])
            return ++counter

        // just to show you the different key 
        console.log(`the ${key} is not the same`);
        return counter;
    }, 0);
    return (count / Math.min(firstLength, secondLength)) * 100;
};

console.log(findMatch(a, b)); // expected result = 70? 
//                            WRONG ^^ it's 60%

and i think you are wrong about the expected output good luck

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda