Any help ? Javascript sum

Hello everyone, well i would like to log the sum of numbers in the hash1 that are not present in the hash2 ! am getting 0 any help ? thank you in advance.

let hash1 = {
  '2': 0,
  '3': 1,
  '4': 2,
  '8': 3,
  '5': 4,
};

let hash2 = {
  '2': 0,
  '7': 1,
  '9': 2,
  '5': 3,
  '1': 4,
}

let totHash = 0;

for(i = 0; hash1.length = 0; i++){
  if(hash1[i] === hash2[i]){
    totHash = totHash + hash1[i] 
  }
}

console.log(totHash)

Hello there,

Have a re-read of this line:

for(i = 0; hash1.length = 0; i++){

Also, how do you expect to go through each number, if each hash is an object literal?

Hope this helps

1 Like

Ah thank you just change it, even it’s first time working with objects exercise, thought it’s the same as arrays, and now am getting stuck

you neef a for…in loop for an object
also, check the hasOwnProperty method

1 Like

Thank you, for your time and answers !!
i found the solution !!

let hash1 = {
    '8': 0,
    '3': 1,
    '4': 2,
    '2': 3,
    '5': 4,
  };
  
  let set2 = [8, 7, 9, 5, 3]
  
  let totHash = 0;
  
  for(let i = 0; i < set2.length ; i++){
    
    if(hash1[set2[i].toString()] !== undefined){
      console.log(set2[i])
      totHash += set2[i]
    };
    
  }
  totHash = totHash*2;
  
  console.log(totHash)