Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exercise : Second Cheapest Book

Second-Cheapest Book

Given an array of book objects, return the book object with the second-cheapest price. eg.

CODE TEMPLATE


function secondCheapest(bookObj){
// write your code here
}
console.log(secondCheapest([
    {title:'Fox In Socks', price:32.20},
    {title:'The Cat In The Hat', price:16.20},
    {title:'Green Eggs and Ham', price:28.50},
    {title:'Thinking Fast and Slow', price:58.80},
    {title:'War and Peace', price: 5.20}
]));

should return the {title:'The Cat In The Hat', price:16.20}, in the input list.

Note:

  • If the array is empty, return undefined
  • If there is only one item in the array, return that item
  • All books will have a different price
  • Do not sort the list
  • The return value must point to an object in the original list, not a copy of that object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment