How to Solve Programming Problems - evan-401-advanced-javascript/seattle-javascript-401d31 GitHub Wiki
The article for "How to solve Programming Problems" offers a repeatable approach for how we should be solving problems even though it goes a bit against our instincts. Speaking for myself, when I get a problem to solve I read through it once or twice and then begin to try coding it immediately. The article argues that we should be spending most of our time understanding the problem and framing our solution before we write a single line of code. The steps look like this:
- Read the problem completely twice.
- Solve the problem manually with 3 sets of sample data.
- Optimize the manual steps.
- Write the manual steps as comments or pseudo-code.
- Replace the comments or pseudo-code with real code.
- Optimize the real code.
For example let's say we are writing code that will take in a string and will push that string to an array one time for each letter and will have removed one letter from the end of the string each time. If the word was pencil the array would look like: ['pencil', 'penc', 'pen', 'pe', 'p'].
First go through steps 1-3 which should take up the majority of the time. Then it is time for the pseudo code.
Step 4
String Array = [] Loop through the array for each letter in the string Push the string with one less letter on the end then the last time you pushed it to the array
Step 5
let strArray = []; for(let i = 0; i <= str.length; i++) { strArray.push(str.slice(i, str.length))
By following this process you should have already solved the problem before you write your first line of code.