Sep 7, 2018 · 1 min read
I had seen a .split() method in Python before, but when I did this exercise, I didn’t realize that JavaScript had such a function. I most definitely would have used it, had I realized that JavaScript had such a method, but I don’t recall it in the exercises. So, instead, I came up with the following:
- Write a function to find the first word in a string (assuming that there does not exist any white-space before the word).
- Write a function which replaces a string S with a sub-string S* of S, such that S* contains everything past the first white-space in S. In other words, write a function to produce a sub-string of a string which doesn’t contain the first word of the original string.
- Then using those two functions, write a function to convert a string into an array A of strings of the words.
- Then write a function to find the longest word W in a string via A.
- Finally, write a function to find the length of W.
I’ll table that solution to my CodePen here. Number 1. is my third function “firstWord”. I also wrote tests, which I’ve commented out there.