--- id: 59c3ec9f15068017c96eb8a3 title: Farey sequence challengeType: 1 forumTopicId: 302266 dashedName: farey-sequence --- # --description-- The Farey sequence Fn of order `n` is the sequence of completely reduced fractions between `0` and `1` which, when in lowest terms, have denominators less than or equal to `n`, arranged in order of increasing size. The *Farey sequence* is sometimes incorrectly called a *Farey series*. Each Farey sequence: The Farey sequences of orders `1` to `5` are: # --instructions-- Write a function that returns the Farey sequence of order `n`. The function should have one parameter that is `n`. It should return the sequence as an array. # --hints-- `Farey` sollte eine Funktion sein. ```js assert(typeof farey === 'function'); ``` `Farey(3)` sollte ein Array zurückgeben ```js assert(Array.isArray(farey(3))); ``` `farey(3)` sollte `['0/1','1/3','1/2','2/3','1/1']` zurückgeben ```js assert.deepEqual(farey(3),['0/1', '1/3', '1/2', '2/3', '1/1']); ``` `farey(4)` sollte `['0/1','1/4','1/3','1/2','2/3','3/4','1/1']` zurückgeben ```js assert.deepEqual(farey(4), ['0/1', '1/4', '1/3', '1/2', '2/3', '3/4', '1/1']); ``` `farey(5)` sollte `['0/1','1/5','1/4','1/3','2/5','1/2','3/5','2/3','3/4','4/5','1/1']` zurückgeben ```js assert.deepEqual(farey(5), [ '0/1', '1/5', '1/4', '1/3', '2/5', '1/2', '3/5', '2/3', '3/4', '4/5', '1/1' ]); ``` # --seed-- ## --seed-contents-- ```js function farey(n) { } ``` # --solutions-- ```js function farey(n) { const sequence = [{ string: "0/1", float: 0.0 }]; for (let i = 1; i < n; i++) { for (let j = n; j >= i; j--) { if (i === 1 || j % i > 0) { sequence.push({ string: `${i}/${j}`, float: i / j }); } } } return sequence .sort((a, b) => a.float - b.float) .map(e => e.string) } ```