--- id: 59c3ec9f15068017c96eb8a3 title: Farey sequence challengeType: 5 forumTopicId: 302266 dashedName: farey-sequence --- # --description-- The [Farey sequence](https://en.wikipedia.org/wiki/Farey sequence "wp: 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` should be a function. ```js assert(typeof farey === 'function'); ``` `farey(3)` should return an array ```js assert(Array.isArray(farey(3))); ``` `farey(3)` should return `["1/3","1/2","2/3"]` ```js assert.deepEqual(farey(3), ['1/3', '1/2', '2/3']); ``` `farey(4)` should return `["1/4","1/3","1/2","2/4","2/3","3/4"]` ```js assert.deepEqual(farey(4), ['1/4', '1/3', '1/2', '2/4', '2/3', '3/4']); ``` `farey(5)` should return `["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]` ```js assert.deepEqual(farey(5), [ '1/5', '1/4', '1/3', '2/5', '1/2', '2/4', '3/5', '2/3', '3/4', '4/5' ]); ``` # --seed-- ## --seed-contents-- ```js function farey(n) { } ``` # --solutions-- ```js function farey(n){ let farSeq=[]; for(let den = 1; den <= n; den++){ for(let num = 1; num < den; num++){ farSeq.push({ str:num+"/"+den, val:num/den}); } } farSeq.sort(function(a,b){ return a.val-b.val; }); farSeq=farSeq.map(function(a){ return a.str; }); return farSeq; } ```