Hi @hassanuddinishak!
Learning how to solve algorithms takes time to get used to.
It is a whole other skill that you have to develop. It is normal for a lot of people to struggle with these in the beginning.
Here is my process for tackling algorithms.
Step 1:
Paraphrase the problem in your own words
It is really important that you understand the problem. You don’t want to be coding for 20 minutes and realize that you misunderstood the problem and have to start over.
Step 2:
Solve the problem without code
Grab some pen and paper (or whiteboard) and solve the problem using one of the test cases.
For example, one of the earlier challenges is to reverse a string.
Forget about the code for a second and ask yourself “How would a human solve this?”
Well you take the end characters of the original word and place them in the beginning of the new reversed word, right? You don’t need any code at this stage.
Step 3:
Write down what you did step by step
Sticking with the reverse a string problem my steps would be.
-
create a new variable for reversed word
-
start at end of the old word and grab the last character
-
Place it in the beginning of the new reversed word.
-
Continue the process of working end to beginning of old word grabbing one letter at a time and placing it in the new word.
There are many methods to solve this problem but that is the basic idea.
Step 4: Translate your instructions from step 3 into code
Sometimes it will be helpful to have mdn docs open to see what helpful methods you can use to solve some of these problems.
Hope that helps