Skip to content

#306 Minimal4

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

306 Minimal4

Write a function that returns the first truthy argument passed to the function.

If all arguments are falsy, return the string "not found".

The function will be called with a minimum of one and a maximum of four arguments: a, b, c, d.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/306 Minimal4.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.

//Write a function that returns the first truthy argument passed to the function.
//If all arguments are falsy, return the string "not found".
//The function will be called with a minimum of one and a maximum of four arguments: a, b, c, d.
function firstOne(a, b = 0, c = 0, d = 0) {
    return a || b || c || d || "not found";
}
import { assertEquals } from '../testHelper.js';
const firstTruthyCases = [
    [["Edabit"], "Edabit"],
    [[false], "not found"],
    [["", "Pikachu"], "Pikachu"],
    [["", 0], "not found"],
    [[0, 20, ""], 20],
    [[0, 0, ""], "not found"],
    [["", 0, true, false], true],
    [["", 0, false, true], true]
];

for (const [args, expected] of firstTruthyCases) {
    assertEquals(firstOne(...args), expected)
}

Clone this wiki locally