Let’s tackle the first question of Javascript DSA, question goes like this.
“Given a string, return a new string with the reversed order of characters”
So in this question, we will need to create a function that takes a parameter of string and it should be giving reversed order. ie, input: hello. Output: olleh.
Let’s see our solution one,
In this soultion, we have converted strings into array, and we have reversed array method. Which is easy method.
But in interview, we mostly get a scenario that we shall not be using reverse() method,
Now on to the solution two
Now in our solution 2, we just created a temporary variable and we have reversed using for loop. Which is clear and good.
Sometimes, you know! interviewer ask you are creating a temporary value. Actually you are not suppose to create it. That exactly happend in one of my interview question.
Hence our solution three, would be
I know it is little confusing, this is the trick to achieve the result in a harder way. Well I have one simple way, we can achive the same by using javascript reduce method.