Random Question I think I found an error, I'm not sure?

Basic Algorithm Scripting: Find the Longest Word in a String

this is the correct:

function findLongestWordLength(s) {
  return s.split(' ')
    .reduce(function(x, y) {
      return Math.max(x, y.length)
    }, 0);

This is what I put that wasn’t correct:

function findLongestWordLength(s) {
  return s.split('')
    .reduce(function(x, y){
      return Math.max(x,y.length)}, 0);

Hope this helps in some way

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Do you mean a bug? Yours is different, look at the argument given to split

If a space is in quotes then it is actually kept any spaces outside of quotes are basically discarded. Generally just do the indentation and line breaks for sanity to be able to quickly see where stuff stops and starts but inside of quotes (when defining a string) the spaces are literally in there and aren’t removed.

Strings are how the regular text on page is represented in the JS version of the page called the JSDOM and spaces need to be preserved for anything shown to the user.

Thanks for the replies, and I hope my experience helps other ppl in the same position.

It’s much simpler than this, you’re mashing up lots of different unrelated things here: " " (ie the space between those quotes) is a character, same as a or 1 or 😬.

Got it, and thanks for the feedback