👨‍💻
Learn to Code
  • Welcome
  • HTML
    • The Basics
    • Getting Started
  • JavaScript
    • The Basics
      • Values
      • Variables
      • Functions
      • Operators
      • Conditional Statements
      • Arrays
      • Loops
    • Intro to the DOM
    • Working with the DOM
    • Activities
      • Text Summariser
        • Getting the words
        • Ranking the sentences
        • Getting the summary
      • Greeter Function
      • Fizz Buzz
      • Temperature Converter
Powered by GitBook
On this page

Was this helpful?

  1. JavaScript
  2. Activities
  3. Text Summariser

Getting the summary

We will first need to sort our ranked sentences by their score. To do this we can use the array sort method and a fairly common numerical comparator.

const sorted = ranked.sort((a, b) => {
  if (a.score < b.score) {
    return 1;
  } else if (a.score > b.score) {
    return -1;
  }
  return 0;
});

This will compare the scores and put the entries in the correct order. Once we have done this, all we need to do is take the n first items, where n is the number of sentences we want, in this case, I will take 5.

const top5 = sorted.slice(0, 5);
console.log({ top5 });

And with that, you should now have a 5 sentence summary of the block of text you provided as input 🎉

PreviousRanking the sentencesNextGreeter Function

Last updated 4 years ago

Was this helpful?