1. 文本转成单词数组

    str.match(/\s*\w+\s*/g)

    str.match(/\w+/g)

    str.split(' ')//单词间是用空格隔开的,split 分离 分裂 划分 分开

  2. 得到单词数组中最长的那个

    a.length > b.length

    Math.max(a.length, b.length, c.length, d.length)

  3. 考虑边界

    只有一个单词的时候,直接返回长度

solution 1

//在下的答案
function findLongestWordLength(str) {
  let words = str.split(' ')
	//算是冒泡了,两辆比较,长的那个参与下一次比较
  let longestWord = words.reduce((a, b) => a.length > b.length ? a : b)
  return longestWord.length;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

//补充边界版
function findLongestWordLength(str) {
  let words = str.split(' ')
	if(words.length == 1)//只有一个单词
		return str[0].length
	//算是冒泡了,两辆比较,长的那个参与下一次比较
  let longestWord = words.reduce((a, b) => a.length > b.length ? a : b)
  return longestWord.length;
}

''.split(' ').reduce((a,b)=>a>b.length?a:b.length)//''
''.split(' ').reduce((a,b)=>a>b.length?a:b.length, 0)//0

solution 2

function findLongestWordLength(str) {
  return Math.max(...str.split(' ').map(word => word.length));;
}

'the part'.split(' ')//['the', 'part']
['the', 'part'].map(a => a.lengt)//[3, 4]
Math.max(...[3, 4])//4

https://www.freecodecamp.org/forum/t/freecodecamp-challenge-guide-find-the-longest-word-in-a-string/16015