When first learning to code if your anything like me you started with printing "Hello World!" in your language of choice.
Then it was variables how to set them. Data types like string, integers and Booleans …etc. Then it was function using the variables and passing them in as arguments and the list goes on.
But one key concept I learned early was iterating. At the time I didn't understand how important this was going to be combine with all the basic I was learning before. So my advice to anyone learning to code is Take Your Time really get a good feel for the basic before moving forward.
For my example were going to use JavaScript as our language and were simply going to be looking at a reverse string algorithm problem and connecting the dots.
This is an example of how a for loop is set up:
for (initializer; condition; iterator) {
// statements
}
Here is an example of a for loop in action with a simple reverse string algorithm problem.
In this example we have a combination of the basic on display.
- We have a variable set to an empty string.
- We have a function with an argument of str (short for string) -which is a data type.
- We are iterating over the string
- Returning the string in reverse.
function reverseString(str) {
let reversed = "";
for (var i = str.length - 1; i >= 0; i--){
reversed += str[i];
}
return reversed;
}
As I continue to start working on learning different algorithms. It is clear that these simple concepts are actually really important to the being able to do complex things in coding.
As I have learned. You really have to crawl before you walk.
I hope this helps and as always.
Happy Coding 👨🏿💻👨🏻💻👩💻🧑🏾💻
Top comments (0)