When I face a problem that I need to turn into code an I’m not sure where to start, I do four things:
I look at the problem, and I think how it relates to a problem in real life.
For example, if the problem is
write a function that takes two strings as parameters, and compares them; if they are the same return true. I would think, this is similar to a real life problem of, say, looking at a recipe, and checking to see if an ingredient is the same as one in my fridge.
You could take the problem, and see how you do it on paper. Take the above example. You could write it out on paper, and see how you would do it, if you had to do the task. (Not write a program to do it, but. if you personally had to do it.) Think about the steps involved in what you’re doing.
Extra steps to take at this stage if you are very stuck
I think how I would do the task in real life. Take the example above. To see whether ingredient names are the same, I would, in theory, check each individual letter, and see if they all match, realistically, I would see if the two words are the same
if (word1 === word2).
- Next, write pseudo-code. Pseudo-code is like instructions for people, written in a similar fashion to code. Take the example of the recipe function. What steps would you take to see if two words are the same? Write that out. In the example above, you might say:
Take two words. If the first word is the same as the second word, it is true.
Pseudo-code is fairly similar to computer code, but written in a way that is natural to you. (If English is not your first language, you can use whichever language you want to-spoken, that is).
- Finally, take your pseudo code, and turn it into real code. You might do this bit by bit, revising the pseudo-code at each interval, or you might do it all at once.
Also, don’t be afraid to revise topics you have previously done. A lot of confusion can be cleared up if you relearn topics, to really cement the knowledge you’ve learnt.
I hope this helps!