hi eric, this is what you can do whenever you get coding problem :
1. Understand the problem completely before attempting to solve it
many newbie camper like to code directly after read task’s explanation without ever looking at input test, only to find their answer unable to fulfill one of input test requirement
2. Solve the problem manually, use sample to check your solution
divide your answer into several manual step, say you want to do “find the longest work” task.
this is what you need to do :
- split input string
- find a way to save the current longest word
- use “for” to compare every word’s length with the current longest word, if word’s length > current longest, replace it
Give it a try with “The quick brown fox jumped over the lazy dog”. “Jumped” will replace previous longest word and can’t be replaced by later word because none of them have more letter.
3. Make pseudo code as a guide
Let’s move to java script and start making some comment, it really helps in the future, especially if you want to use this code as reference for yourself and for another people that might use it. Based on previous example, this is what your pseudo code would look like :
//1. split string into array
//2. make number variable to save current longest word,
//3. loop through word to find longest word
//4. return longest word variable
4. Make the real code
Say you don’t how to do 3rd, that won’t stress you much because that’s only 1/4 of your later program. even if you need to google some answer, you know exactly what are you looking for.
This is also where experience matter.If it’s not working, use sample input to check which step didn’t give intended result
5. Optimize your code
Step 1, 2, and 4 is pretty simple, no need to improve it. For step 3 you can try with Math.max().
This step seem unnecessary for earlier challenge, but trust me, it saved me lot of time in medium challenge
nothing more frustating than fail a challenge because you misread the description