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?
CodePudding user response:
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