Skip to main content
Removes unrelated "alternate solution" (Math.max)
Source Link
// Make use of `map` and `reduce`
const longestWordLength = str => str.split(' ')
  .map(w => w.replace(/\W/, ''))
  .reduce((longest, current) => (current.length > longest.length) ? current : longest)
  .length;

// OR

// Make use of `map` and `Math.max`
const longestWordLength = str => Math.max(
  ...str.split(' ')
    .map(w => w.replace(/\W/, ''))
    .map(w => w.length)
);


// Either way...
longestWordLength("Mary had a little lamb.");
// => 6
// Make use of `map` and `reduce`
const longestWordLength = str => str.split(' ')
  .map(w => w.replace(/\W/, ''))
  .reduce((longest, current) => (current.length > longest.length) ? current : longest)
  .length;

// OR

// Make use of `map` and `Math.max`
const longestWordLength = str => Math.max(
  ...str.split(' ')
    .map(w => w.replace(/\W/, ''))
    .map(w => w.length)
);


// Either way...
longestWordLength("Mary had a little lamb.");
// => 6
// Make use of `map` and `reduce`
const longestWordLength = str => str.split(' ')
  .map(w => w.replace(/\W/, ''))
  .reduce((longest, current) => (current.length > longest.length) ? current : longest)
  .length;

longestWordLength("Mary had a little lamb.");
// => 6
restores original answer for reference to "simple"
Source Link

Original answer (shortest)

someString.split(' ')
  .reduce((max, word) => word.length > max ? word.length : max, 0);

Short version/solution (stripped of comments/etc...)

Short version/solution (stripped of comments/etc...)

Original answer (shortest)

someString.split(' ')
  .reduce((max, word) => word.length > max ? word.length : max, 0);

Short version/solution (stripped of comments/etc...)

added 91 characters in body
Source Link
// Make use of `map` and `reduce`
const longestWordLength = str => str.split(' ')
  .map(w => w.replace(/\W/, ''))
  .reduce((longest, current) => (current.length > longest.length) ? current : longest)
  .length;

// OR

// Make use of Math`map` and `Math.maxmax`
const longestWordLength = str => Math.max(
  ...str.split(' ')
    .map(w => w.replace(/\W/, ''))
    .map(w => w.length)
);


// Either way...
longestWordLength("Mary had a little lamb.");
// => 6

Longer version/solution and some of the "why". Please refer to comments/documentation.

Note, that I am accounting for punctuation and not counting it as part of the word.

// Make use of `reduce`
const longestWordLength = str => str.split(' ')
  .map(w => w.replace(/\W/, ''))
  .reduce((longest, current) => (current.length > longest.length) ? current : longest)
  .length;

// OR

// Make use of Math.max
const longestWordLength = str => Math.max(
  ...str.split(' ')
    .map(w => w.replace(/\W/, ''))
    .map(w => w.length)
);


// Either way...
longestWordLength("Mary had a little lamb.");
// => 6

Longer version/solution and some of the "why". Please refer to comments/documentation.

// Make use of `map` and `reduce`
const longestWordLength = str => str.split(' ')
  .map(w => w.replace(/\W/, ''))
  .reduce((longest, current) => (current.length > longest.length) ? current : longest)
  .length;

// OR

// Make use of `map` and `Math.max`
const longestWordLength = str => Math.max(
  ...str.split(' ')
    .map(w => w.replace(/\W/, ''))
    .map(w => w.length)
);


// Either way...
longestWordLength("Mary had a little lamb.");
// => 6

Longer version/solution and some of the "why". Please refer to comments/documentation.

Note, that I am accounting for punctuation and not counting it as part of the word.

Adds much more comprehensive code examples and links to relevant learning resources
Source Link
Loading
Source Link
Loading