Skip to content

#299 Comparing Arrays

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

299 Comparing Arrays

Create a function that returns true if two arrays sharing the same length have identical numerical values at every index, and false otherwise.

Fix this code

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/299 Comparing Arrays.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function that returns true if two arrays sharing the same length have identical numerical values at every index, and false otherwise.
// Fix this code
function checkEquals(arr1, arr2) {
    return arr1.length === arr2.length &&
        arr1.every((value, index) => value === arr2[index]);
}
import { assertEquals } from '../testHelper.js';
// test cases
const actual = [checkEquals([1, 2], [1, 2]), checkEquals([1, 2], [1, 3]),
checkEquals([11, 2], [1, 12]), checkEquals([34, 56, 98], [34, 56, 98]),
checkEquals([1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7], [11, 12, 13, 14, 15, 16, 17]),
checkEquals([11, 12, 15, 16, 19], [11, 13, 15, 1, 6, 19, 20]),
checkEquals([11, 12, 13, 14, 15, 16], [11, 12, 13, 14, 15, 16]), checkEquals([123, 45, 6, 78, 90], [1234, 5678, 90]),
checkEquals([23, 26, 78, 934], [94, 26, 78, 934]), checkEquals([], []),
checkEquals([9, 87, 1, 23], [98, 7, 12, 3])]

// results
const expected = [true, false, false, true, false, false, true, false, false, true, false]

// messages
const message = ["", "", "Are you sure this is true?", "", "Should return false", "", "", "", "", "", ""]

// run tests
for (const i in actual) {
	assertEquals(actual[i], expected[i], message[i])
}

Clone this wiki locally