2

I have the following code that is supposed to find the intersection between two strings in an array like this ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"] It should give the result: 1,4,13

  function FindIntersection(strArr) { 

    const firstArr = strArr[0].split(", ");
    const secondArr = strArr[1].split(", ");

    let newArr = [];
    let i = 0;
    let j = 0;


    while(i < firstArr.length && j < secondArr.length) {
      let a = firstArr[i] | 0;
      let b = secondArr[j] | 0;
      if(a === b) {
        newArr.push(a);
        i++;
        j++;
      } else if(a > b) {
        j++;
      } else if (b > a) {
        i++;
      }
    }

    strArr = newArr.join(",");

    return strArr; 

}

When I do not use the bitwise operator | 0 the last element in the array is not accessed properly why? How does the bitwise operator solve this problem?

Benedikt
  • 608
  • 6
  • 27

3 Answers3

2

You don't need a bitwise operator explicitly, you just need to convert it to an integer. As mentioned in comments, it's just one way of triggering a cast of a string into an integer.

typeof('str' | 0) === 'number'

But that sucks, right, since 'str' there is probably a bug. I'd be more explicit and use:

let a = parseInt(firstArr[i], 10);

That will return NaN if it is not a valid integer, and NaN === NaN is false, so you won't accidentally add it to newArr.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
daphtdazz
  • 7,754
  • 34
  • 54
2

To be clear, you're not accessing the element of an array with the bitwise operator, you're accessing the element with the numbers in i and j and then you're using the bitwise OR to convert the element into a number.

A few other ways to convert a string that's a number into a number:

"3" | 0 -> 3
"3" * 1 -> 3
+"3" -> 3
parseInt("3") -> 3
Nate
  • 6,384
  • 3
  • 25
  • 30
1

Its convert string to number, with parseInt works too

  function FindIntersection(strArr) { 

    const firstArr = strArr[0].split(", ");
    const secondArr = strArr[1].split(", ");

    let newArr = [];
    let i = 0;
    let j = 0;


    while(i < firstArr.length && j < secondArr.length) { 
      let a = parseInt(firstArr[i]);
      let b = parseInt(secondArr[j]);
      if(a === b) {
        newArr.push(a);
        i++;
        j++;
      } else if(a > b) {
        j++;
      } else if (b > a) {
        i++;
      }
    }

    strArr = newArr.join(",");

    return strArr; 

}

By this way works too, by using the Number object

  function FindIntersection(strArr) { 

    const firstArr = strArr[0].split(", ");
    const secondArr = strArr[1].split(", ");

    let newArr = [];
    let i = 0;
    let j = 0;


    while(i < firstArr.length && j < secondArr.length) { 
      let a = Number(firstArr[i]);
      let b = Number(secondArr[j]);
      if(a === b) {
        newArr.push(a);
        i++;
        j++;
      } else if(a > b) {
        j++;
      } else if (b > a) {
        i++;
      }
    }

    strArr = newArr.join(",");

    return strArr; 

}

console.log(FindIntersection(["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]));
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35