Topic describes

(Path cycle method)

  1. Remove whitespace from both sides of the string
  2. Split it based on space, and filter out the space in the middle
  3. Invert the array and join it

The problem solving code

var reverseWords = function(s) {
    // Remove whitespace from both sides of the string
    s = s.trim();
    // Split based on space, filter out the middle space after split
    const temp = s.split(' ').filter(value= >value ! = =' ')
    // Invert the array and join it
    return temp.reverse().join(' ')};Copy the code

Conclusion (this topic gives us the enlightenment of thinking)

  • Lesson 1: Use trim to remove whitespace from both sides of a string. This function does not affect the original string, so you need to do the assignment yourself
  • Lesson 2: Learn to use filter to filter arrays
  • Lesson 3: Use reverse to reverse arrays
  • Lesson 4: Learn to join arrays into strings using joins
  • You need to be familiar with common apis