Skip to content

#021 The Farm Problem

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

021 The Farm Problem

In this challenge, a farmer is asking you to tell him how many legs can be counted among all his animals.

The farmer breeds three species:

chickens = 2 legs

cows = 4 legs

pigs = 4 legs

The farmer has counted his animals and he gives you a subtotal for each species.

You have to implement a function that returns the total number of legs of all the animals.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/021 The Farm Problem.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.

//In this challenge, a farmer is asking you to tell him how many legs can be counted among all his animals.
//The farmer breeds three species:
//chickens = 2 legs
//cows = 4 legs
//pigs = 4 legs
//The farmer has counted his animals and he gives you a subtotal for each species.
//You have to implement a function that returns the total number of legs of all the animals.
function animals(chickens, cows, pigs) {
    return chickens * 2 + cows * 4 + pigs * 4;
}
import * as Test from '../testHelper.js';
Test.assertEquals(animals(5, 2, 8), 50)
Test.assertEquals(animals(3, 4, 7), 50)
Test.assertEquals(animals(1, 2, 3), 22)
Test.assertEquals(animals(3, 5, 2), 34)

Clone this wiki locally