# Checking array equality in confirmEnding function

Hi all,

I’m tackling the confirmEnding challenge in Basic Algorithm Scripting and decided to make two arrays:

• an array of each letter from the target: […target]
• an array of the last letters of the str argument, equal in length to that of the target.

The code is doing what I want, in that if you pass the arguments (‘Chair’, ‘ir’) you get two identical arrays of [‘i’, ‘r’], and if you pass (‘Chair’, ‘xx’) you get one array of [‘i’, ‘r’] and the other of [‘x’, ‘x’]

But in my last step of code when I run an equality check of the arrays, it always returns false, even if they are identical. Hope this question makes sense and here’s the code below. Please advise.

``````function confirmEnding(str, target) {
let targetArr = []
let targetBreakdown = [...target];
for (let i = str.length - target.length; i < str.length; i++){
targetArr.push(str[i])
}
return targetBreakdown === targetArr
}

console.log(confirmEnding("Open sesame", "same"));
``````

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks! I’d been wondering how everyone was doing that.

I can see what you mean when you wrote

but you just can’t compare two arrays like that.

The explanation is somewhat long, so hold on to your chair!

``````const myFirstArr = [1, 2, 3];
const mySecondArr = [1, 2, 3];
``````

We have two arrays with the same contents, right?

So are they the same array?

``````const myFirstArr = [1, 2, 3];
const mySecondArr = [1, 2, 3];

console.log(myFirstArr === mySecondArr);
``````

The console says NO!

This is due to how JavaScript manages more complex (non-primitive) data types.

Roughly speaking, a variable can only hold one value. But an array (or an object) has multiple values, so how does this work?

Well, the JavaScript engine stores the entire array (or object) in a place in your computer’s memory that is larger than the space allocated to a singe JavaScript variable. Then, the JavaScript engine stores the location of the array (or object) into the variable.

So, going back to our example,

``````const myFirstArr = [1, 2, 3];
const mySecondArr = [1, 2, 3];

console.log(myFirstArr === mySecondArr);
``````

this asks if the location the first array is stored in is the exactly the same as the location the second array is stored in.

Well, they are two separate arrays, so the locations they are stored in are different, and JavaScript (correctly) tells us that these two variables are different.

But if we check

``````const myFirstArr = [1, 2, 3];
const mySecondArr = [1, 2, 3];

console.log(myFirstArr[0] === mySecondArr[0]);
// ... and so on
``````

We can verify that the contents of the two arrays are the same.

So… you need to check that the contents match instead of the locations.

2 Likes

Got it! And thanks for the quick response.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.