Created
August 3, 2011 02:21
-
-
Save subtleGradient/1121755 to your computer and use it in GitHub Desktop.
Write the body of the sortByFoo function below
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Write the body of the sortByFoo function below. | |
| * Executing the code must display no warnings in your browser's console. | |
| * i.e. all the assertions must pass | |
| */ | |
| function sortByFoo(array){ | |
| // TODO: Write code here. Use Array.sort. | |
| return array; | |
| } | |
| var array | |
| array = [{foo: 5}, {foo: 7}, {foo: 4}, {foo: 3}, {foo: 2}, {foo: 1}, {foo: 6}]; | |
| console.assert(sortByFoo(array)[5].foo === 6, | |
| "Must sort by .foo attribute"); | |
| array = [] | |
| console.assert(sortByFoo(array) === array, | |
| "Must not crash on an empty array"); | |
| array = [{foo: 42}, {bar: 7}, {foo: 5}] | |
| console.assert(sortByFoo(array)[1].foo === 5, | |
| "If a `foo` attribute is missing, it is considered equal to '0'"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment