Argmax CLI
A Node CLI that reads a comma-separated number list from argv and prints the index of the maximum value, with ties going to the first.
Project
node-argmax-cli
Entry point: argmax.mjs
Install
node --versionRun
node argmax.mjs 3,1,4,1,5,9,2,6Test
node --test argmax.test.mjsExpected output
Prints the zero-based index of the maximum value, for example 5 for the input 3,1,4,1,5,9,2,6.Dependencies
- node >=20
What it does
Reads a comma-separated list of numbers from the first command-line argument, scans for the maximum, and prints its zero-based index. Ties go to the first occurrence because the comparison is strict greater-than.
const nums = process.argv[2].split(',').map(Number);
let max = -Infinity, idx = 0;
for (let i = 0; i < nums.length; i++) if (nums[i] > max) { max = nums[i]; idx = i; }
console.log(idx);
Run and verify
node argmax.mjs 3,1,4,1,5,9,2,6
Run the test command to check the implementation: node --test argmax.test.mjs.
Expected Output
For the input 3,1,4,1,5,9,2,6 the maximum is 9 at index 5:
5
Limitations
All values must parse as finite numbers; a non-numeric token prints NaN and the comparison treats NaN as not greater than anything.