본문 바로가기

Algorithms

freeCodeCamp - 4. Find the Longest Word in a String [Basic Algorithm Scripting]

Find the Longest Word in a String

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string

 

www.freecodecamp.org

 

so1)

function findLongestWordLength(str) {
  let arr = str.split(' ')
  let arr2 = []
  for(let a of arr){
    arr2.push(a.length)
  }
  return Math.max(...arr2)
}

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

 

sol2)

function findLongestWordLength(str) {
  let arr = str.split(' ')
  let max = 0
  for (let a of arr){
    if(a.length >= max){
      max = a.length
    }
  }
  return max
  return str.length;
}

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