As somebody who learnt coding by copying and pasting snippets from anywhere on the internet, I get stuck quite a lot when working on projects as that's not exactly the best way to learn.
But after almost 2 years of coding, I've gotten better and along the way made a mental checklist of things to do when I hit a new pothole to get back on track. I don't know what expert coders with CS degrees do when they're stuck so this may not be the best list. But if it helps anyone else, here goes:
1. Check for missing brackets.
I used to code in an editor in which I couldn't see missing brackets until the app would crash and the error message would mention something about a missing curly brace. I've since shifted to VS code that does the job for me before I run the code. Atom also has some great plugins that help you.
2. Change the order
Sometimes it will make no sense at all, and sometimes it will make all the sense in the world. But oftentimes just moving some lines of code back and forth does the trick.
3. Add lots of console messages
I saw an article on Medium the other day about how you should never use console.log. I think that's ridiculous. I'm sure even the experts use them.
Add as much console.log messages as you can in the functions that is crashing. Fit it in between steps. Add variables to the messages to see what they are at each step before processing.
4. Search the error message
Sometimes it's not even your fault. Might be some bug in the package or framework you're using. A quick Google search will show somebody else in the same predicament and an Einstein or two that would have already solved it.
5. Understand the code
If all else fails I go back to the documentation and try and understand every bit of the code I'm running. It helps. I used to do this more often in the beginning when I was learning programming. Because it also helps give me more technical terms for the stuff I'm working with.
Putting that in Google gives better search results than what you'd get with a description of the crash.
I'd love to hear what everyone else does when they're stuck programming. Also, if you're interested in seeing where I invest my time coding, check out my site: thiscodeworks.com