2

I am working on the following problem: Write a function called same, which accepts two arrays. The function should return true if every value in the array has it's corresponding value squared in the second array. The frequency of the values must be the same.

The solution seems to be working except for the part where I compare the second array to the third array. Console log tells me they are the same but the comparison fails indicating they are not equal.

Why would second===third be false when they are the same?

function same(first, second) { 
    first.sort((a,b) => a-b)
    console.log(first)

    second.sort((a,b) => a-b)
    console.log(second)

    let third = first.map((a) => a**2)
    console.log(third)

    if (second === third) return true
    else return false
}
$ node same.js
[ 1, 2, 3 ]
[ 1, 4, 9 ]
[ 1, 4, 9 ]
false
Alexius
  • 47
  • 3
  • 2
    same means same primitive value or same object reference. you need to compare item by item of arrays. – Nina Scholz Jan 04 '23 at 11:24
  • What is the full test case? What is the array of array your start with, what should the result be (should it be an array of booleans)? – Harrison Jan 04 '23 at 11:29
  • 2
    To compare arrays check out https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – DeeGee Jan 04 '23 at 11:35
  • Is `[1,2,3]` "same" as `[1,4,9]` only, or is it "same" as `[1,9,4]` too? – tevemadar Jan 04 '23 at 12:29

3 Answers3

1

You're using strict equality (===). In Javascript, objects are compared by reference, not by value. So in your case you're getting false because you're comparing two different objects in memory, they just happen to hold the same values. Depending on the situation, you could either stringify two arrays and compare them (easiest, but not recommneded):

function same(first, second) { 
  first.sort((a,b) => a-b)
  second.sort((a,b) => a-b)

  let third = first.map((a) => a**2)

  return second.toString() === third.toString()
} 

Or, you could actually iterate through the arrays and compare each value (preferred solution):

function same(first, second) { 
  first.sort((a,b) => a-b)
  second.sort((a,b) => a-b)

  if (first.length !== second.length) return false

  for (let i = 0; i < first.length; i++) {
    if (first[i] ** 2 !== second[i]) return false
  }

  return true
}

Or, using the every array method:

function same(first, second) { 
  return first
    .sort((a,b) => a-b)
    .map((a) => a**2)
    .every((value, index) => value === second.sort((a,b) => a-b)[index])
}
codemon
  • 1,456
  • 1
  • 14
  • 26
0

In JavaScript comparing two objects/arrays is done by reference and not by value. Hence, Instead of comparing the values contained in the two arrays, JavaScript checks whether they point to the same reference. This means when comparing two arrays in JavaScript, using either the loose or tight equality operators (== or ===), It will always return false.

I think best approach is to compare the arrays by iterating. To achieve that we can either use for loop or Array#every method which tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Live Demo :

const equalsCheck = (arr1, arr2) =>
    arr1.length === arr2.length &&
    arr1.every((v, i) => v === arr2[i]);

const arr1 = [1, 4, 9];
const arr2 = [1, 4, 9];

console.log(equalsCheck(arr1, arr2));
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

Arrays are compared item by item. Replace this:

if (second === third) return true
    else return false
    

with:

return second.every((v,i) => v === first[i])

like so:

function same(first, second) { 
    first.sort((a,b) => a-b)
    console.log(first)

    second.sort((a,b) => a-b)
    console.log(second)

    let third = first.map((a) => a**2)
    console.log(third)

    return second.every((v,i) => v === third[i]);
}

console.log( same([1,2,3], [1,4,9]) );
PeterKA
  • 24,158
  • 5
  • 26
  • 48