Home > Software engineering >  I cant understand short circuit, object key value
I cant understand short circuit, object key value

Time:09-18

There is nothing wrong with the code below. However, issue is I cannot understand this line here frequencyCounter1[iteration] = (frequencyCounter1[iteration] || 0) 1 properly

**frequencyCounter1[iteration]** = (*frequencyCounter1[iteration]* || 0)   1

So this is setting frequencyCounter1[iteration] key to the object but in ITALIC = (frequencyCounter1[iteration] || 0) 1 it is valute to the object

why this code frequencyCounter1[iteration] is a key and value of a key at the same time ??

function same(arr1, arr2) {
  if(!arr1 && arr2) {
    return false
  };

  if(arr1.length !== arr2.length) {
    return false
  };

  let frequencyCounter1 = {};
  let frequencyCounter2 = {};

  for (let iteration of arr1) {
    frequencyCounter1[iteration] = (frequencyCounter1[iteration] || 0)   1
    console.log(frequencyCounter1)
  };
  for (let iteration of arr2) {
    frequencyCounter2[iteration] = (frequencyCounter2[iteration] || 0)   1
  };

  for(let key in frequencyCounter1) {
    console.log(key)
    if (!(key ** 2 in frequencyCounter2)){
    return false
  }
  if (frequencyCounter2[key ** 2] !== frequencyCounter1[key]){
    return false
  }
  }
  return true
};

console.log(same([2,2,2,5,3,6], [4,4,4,25,9,36]))

CodePudding user response:

why this code frequencyCounter1[iteration] is a key and value of a key at the same time ??

It isn't a key. frequencyCounter1 is an object. iteration is a property name.

It isn't really a value either. In one case it is an expression that evaluates as a value.

Nor is it "at the same time". Things in JS happen one at a time, in order.

You could rewrite it as separate statements.

const startingValue = frequencyCounter1[iteration] || 0;
const updatedValue = startingValue   1;
frequencyCounter1[iteration] = updatedValue;

CodePudding user response:

Let me do a small demonstration for you with comments.

const array = ["A", "B", "C"];

const numbers = {}; // currently I am an empty object.

// Here a single element from array will be transferred to a variable element
// and this is repeated until there is an element remaining
for (let element of array) { 

   // Here we're using computed properties for accessing the value of a key present inside this numbers object.
   // Normally we're saying obj["key-name"] or obj.keyName to access the value for that key
   // but when the same key string is stored in a variable we can also pass that variable inside brackets and it will be evaluated into a string.
   // Here number[element] will be equal to number["A"] for the first iteration.
   // So it will be something like:
   // number["A"] = if number["A"] is not null (falsy value) then use this value || otherwise evaluate the right side value of the pipe symbol
   // An intermediate evaluation for understanding could be:
   // number["A"] = (undefined || 0)   1;
   // number["A"] = (0)   1; because first value is falsy OR operator will move ahead and return the right side value (no matter if that is also falsy - in this case it is 0 means falsy)

   numbers[element] = (numbers[element] || 0)   1;
   // Here we're saying that if the value for this key is present just reassign the same, otherwise set it as 1
}


  • Related