When you’re given a task to write a program, you just don’t open up your code editor or codepen and just start hacking away and hope for the best, or do it trial and error. That will be frustrating, and may lead to dead ends.
Think or write down in plain english (or whatever your spoken language is), step-by-step, what you the programmer needs to do. Break big ideas/concepts down into smaller and smaller tasks – still in plain english (or whatever). If you can’t explain to yourself in plain english what you need to do to solve the problem, how can you write a program for it?
After you have your detailed step-by-step procedure (i.e. called a pseudo code), test each step by writing a small test program, just to make sure it works as you intended. (when you’ve gained more experience, this step can be skipped because you already know it works, or how it works.) But even experienced programmers would sometimes write short programs to test proof-of-concept for new problems/solutions they’ve never encountered before. This is also the part where you convert your pseudo-code to proper syntax using whatever language you’ve chosen.
Once youv’e got all the pieces of the puzzle together, then you can just string them up all along and combine them together, and that is your finished computer program.
Example: Requirement is:
Display Weather in your location
Break it down… you need to know your location first, and then find out the weather in your location.
How do you know your location?
Google around for answers… There are several ways to do this, with varying amount of accuracy and compatibility (and maybe even restrictions). You can do reverse IP lookup (a wild guesstimate, but could be close enough). Or you can use 3rd-party services that offer this functionality.
Using Google, you’ll learn that based on your IP address, there are databases you can download, that contains IP address ranges and location data (country, state, town, city). So you can lookup location based on IP address, Or you can use Google API that will spit out your estimated Lat/Long, City, State. No need to maintain or run a database on your end.
Next step: Write a short test program… does this really work? Just use plain console.log() to see if you’re getting data that looks correct. If you’re in state of California, and you get an answer back that you’re located in Tokyo, then obviously something is wrong - you may have an error in your program. Debug.
Once you have that part working, proceed to the next task… how do I get weather information given a lat/long, or city/state? Google around-- research. Then write a small test program… does it work? Does it give an answer that looks/appears to be correct? Can you extract the specific pieces of information you need? Write a test program or use Console to see if you can target and retrieve the text or number you need.
Now that you have the data, how do you display that as text on an html page? Repeat and rinse… follow the same steps as above. Write a small test program and test the concept. Works? Great… go to next step.
No more new steps? Then combine everything above into a logical sequence.
For each step, you may have to break it down into more detailed steps. You chip away at the problem little by little. You need to know each step, explain in detail, how you can arrive at the answer. If you can’t explain each step, in english how to solve this problem, you won’t be able to write the program. – A computer is the dumbest thing in the world. It only works if told step by step what to do. The only thing a computer is good at is follow the programmer’s detailed instructions very very fast.