Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active October 6, 2022 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/2cbc7447865c9bb2a2f1fef47c105314 to your computer and use it in GitHub Desktop.
Save McLarenCollege/2cbc7447865c9bb2a2f1fef47c105314 to your computer and use it in GitHub Desktop.
Nearest Chapter

Create a function that returns which chapter is nearest to the page you're on. If two chapters are equidistant, return the chapter with the higher page number.

Note: You can use only a single for-in loop

function nearestChapter(chapterObj,page){
// write your code here
}
console.log(nearestChapter({
  "Chapter 1" : 1,
  "Chapter 2" : 15,
  "Chapter 3" : 37
}, 10)); //"Chapter 2"


console.log(nearestChapter({
  "New Beginnings" : 1,
  "Strange Developments" : 62,
  "The End?" : 194,
  "The True Ending" : 460
}, 200)); //"The End?"


console.log(nearestChapter({
 "Chapter 1b" : 5,
  "Chapter 1a" : 1
}, 3)); //"Chapter 1b"

Please watch this video for a hint for the problem:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment