Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created August 3, 2011 02:21
Show Gist options
  • Select an option

  • Save subtleGradient/1121755 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/1121755 to your computer and use it in GitHub Desktop.
Write the body of the sortByFoo function below
/*
* 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