Write down your thoughts

Their thinking

The string itself has no reverse() method, so it needs to be turned into an array. Note that the order of the words is constant.

Var reverseWords = function(s) {reverseWords = function(s) {reverseWords = function(s) {reverseWords = function(s) { Let res = arr.map(item=>{return item.split(" ").reverse().join(" ")}) return res.join(" ") };Copy the code

Expansion of knowledge points involved

How to convert between strings and arrays

1. The split method for strings

The split() method splits a String into an array of substrings using the specified delimiter String, with a specified split String determining the location of each split.

Syntax: str.split([separator[, limit]]) Argument: separator-- Specifies a string representing the point at which each split should occur. 'separator' can be a string or regular expression. If the plain text delimiter contains more than one character, the entire string must be found to represent the split point. If the delimiter is omitted or not present in STR, the returned array contains an element consisting of the entire string. If the delimiter is an empty string, the array of each character in the original STR string is returned. Limit - an integer that limits the number of fragments returned. When provided, the split method splits the string on each occurrence of the specified delimiter, but stops when the restricted item has been put into the array. If the end of the string is reached before the specified limit is reached, it may still contain fewer entries than the limit. The rest of the text is not returned in the new array.Copy the code
2. Array map method

The map method calls the callback function once, in order, for each element in the array. The return values (including undefined) from each callback are combined to form a new array. Callback is called only on indexes that have values; Indexes that have never been assigned a value or deleted with delete are not called.

Grammar:  var new_array = arr.map(function callback(currentValue[, index[, array]]) { // Return element for new_array }[, ThisArg]) parameter: callback Generates a new array element function with three arguments currentValue array being processed current element index index value of the current element being processed arr map method return value of array: A new array consisting of the result of each element of the original array executing the callback function.Copy the code

Example:

Var numbers = [1, 4, 9]; var roots = numbers.map(Math.sqrt); // Roots is [1, 2, 3], numbers is still [1, 4, 9]Copy the code
3. The reverse method for arrays

The reverse() method reverses the position of the elements in the array and returns the array. The first element of the array becomes the last, and the last element of the array becomes the first. This method changes the original array.

Syntax: arr.reverse() Returns the array reversedCopy the code

Example:

const a = [1, 2, 3]; console.log(a); // [1, 2, 3] a.reverse(); console.log(a); / / [3, 2, 1)Copy the code
4. Join method of array

The join() method joins all the elements of an array (or an array-like object) into a string and returns the string. If the array has only one item, that item is returned without a delimiter.

Syntax: arr.join([separator]) Argument: 'separator' Optionally specifies a string to separate each element of the array. Convert the delimiter to a string if necessary. If it defaults, array elements are separated by commas (', '). If 'separator' is an empty string (' "" '), there is no character return value between all elements: a string to which all array elements are joined. If arr.length is 0, an empty string is returned.Copy the code

Example:

var a = ['Wind', 'Rain', 'Fire']; var myVar1 = a.join(); // myVar1 = "Wind,Rain,Fire" var myVar2 = a.join(', '); // myVar2 = "Wind, Rain, Fire" var myVar3 = a.jin (' + '); // myVar3 = "Wind + Rain + Fire" var myVar4 = a.join("); // myVar4 changes to "WindRainFire"Copy the code