0

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]))
  • You must understand that "(x || 0)" means "0 is x is null or undefined, x otherwise". It's an easy shortcut to initialize the variable. – Wolf D. Sep 18 '22 at 13:16
  • i think the problem with me here is that "how an earth characters of an arr1 or arr2 is being transferred to the empty object frequencyCounter1 if frequencyCounter1[iteration] is value to the already appeared key ? – Akhror Khamidov Sep 18 '22 at 13:34
  • initally the array is empty so if you merely write frequencyCounter1[iteration]++ it will result in an error. You need to initialize the value first, thus the (...|| 0) syntax. – Wolf D. Sep 18 '22 at 13:41

2 Answers2

1

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
}


Piyush Pranjal
  • 414
  • 4
  • 11
  • Why the parenthesis is there? `const a = 2 * 3 + 4;` What could be the value of `a` by your understanding? If you don't consider or know about the operator precedence concept. The answer could be `10` or `14` right? But if you simply wrap some of the calculations inside parenthesis you don't need to take care of operator precedence. By default the calculation inside the parenthesis will be evaluated first. – Piyush Pranjal Sep 18 '22 at 13:46
  • Thank you for your time and effort to make it easy for me. I have never thought of this simpel operator precedence, because I never used one when assigning a value to a variable. I understand everything in this whole algorithm but numbers[element] = numbers[element] is undefined, i can digest this one. how come a value which is equal to itself is null or undefined ?! I sitting in front of my computer for like 10 hours. I know it is just simple thing but maybe I am too dumb for math and algorithms. Thank you) – Akhror Khamidov Sep 18 '22 at 13:58
  • Bro. We are not having the values on both sides of the assignment operator. It is like the one which is on the left side of the operator is a variable name and whatever on the right side is the evaluated value of that expression. – Piyush Pranjal Sep 18 '22 at 14:02
  • `number[element] = number[element]` is not means undefined = undefined . It is like `let a = 5;` see on the left side is a variable and on the right side is a value for that variable. Similarly `number[element] = number[element];` is `number.A = undefined;` – Piyush Pranjal Sep 18 '22 at 14:04
  • Anyways don't give up we all faced such situations for some silly issues. You are also stuck in a silly confusion don't worry just come back to this thread or try understanding it after 2 days. You'll get it. – Piyush Pranjal Sep 18 '22 at 14:07
  • 1
    Thank you for you time and effort to explain. I got it. Thank you so much – Akhror Khamidov Sep 19 '22 at 07:19
0

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;
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks for you answer, but i didnt get it. And why this is in parantheses and behaves abnormally ? is there if statement behind the scene. it is like if a key "5" already exist inside the frequencyCounter1 do not add another "5 " instead add value 2 as a count of how many identical keys inside one object ??? (frequencyCounter1[iteration] || 0) + 1 – Akhror Khamidov Sep 18 '22 at 13:19
  • "why this is in parantheses" — Because the `+` operator has higher precedence than the `||` operator – Quentin Sep 18 '22 at 13:31
  • "behaves abnormally" — It doesn't – Quentin Sep 18 '22 at 13:31
  • "is there if statement behind the scene" — Not really. – Quentin Sep 18 '22 at 13:31
  • " it is like if a key "5" already exist inside the frequencyCounter1 do not add another "5 " instead add value 2 as a count of how many identical keys inside one object " — No. You can't have duplicate keys. It does what I said it did. Perhaps you also need to read [this question](https://stackoverflow.com/questions/6439579/what-does-var-foo-foo-assign-a-variable-or-an-empty-object-to-that-va). – Quentin Sep 18 '22 at 13:32