Skip to content

#273 Array Index

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

273 Array Index

Given a 2D-list of letters arr and a list of indexes idx, find the letters on given indexes and return as a string.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/273 Array Index.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.

//Given a 2D-list of letters arr and a list of indexes idx, find the letters on given indexes and return as a string.
function arrIndex(arr, idx) {
    return idx.map(i => {
        const row = Math.floor((i - 1) / arr[0].length);
        const col = (i - 1) % arr[0].length;
        return arr[row][col];
    }).join('');
}
import { assertEquals } from '../testHelper.js';
const results1 = arrIndex([['m', 'u', 'b'], ['a', 's', 'h'], ['i', 'r', '1']], [1, 3, 5, 8])
assertEquals(results1, 'mbsr')
const results2 = arrIndex([['m', 'y', 'e'], ['x', 'a', 'm'], ['p', 'l', 'e']], [1, 2, 3, 4, 5, 6, 7, 8, 9])
assertEquals(results2, 'myexample')
const results3 = arrIndex([['m', 'y', 'e'], ['x', 'a', 'm'], ['p', 'l', 'e']], [1, 5, 6])
assertEquals(results3, 'mam')
const results4 = arrIndex([['m', 'y', 'e'], ['x', 'a', 'm'], ['p', 'l', 'e']], [1, 3, 7, 8])
assertEquals(results4, 'mepl')

Clone this wiki locally