未加星标

String Reversal in Javascript

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

String reversal is a very common interview question, and luckily it’s quite simple. There are many different ways to do this, but here are four methods, starting from the simplest and going to the most complex.

Use arrays

This is a very straightforward method that leverages existing language features in order to accomplish a task. Most interviewers would ask you to perform the same task in a more ‘manual’ way if you tried this, but it demonstrates an awareness of javascript, and is what I’d use in real life to accomplish this.

Javascript allows you to call the reverse method on arrays. You can see some examples in the documentation here .

So we would create a function that does the following:

Converts the string to an array Reverses the array Transforms the array back into a string and returns it reverse = str => { return str .split("") .reverse() .join(""); };

Calling reverse('Ruairidh') would return hdiriauR for example.

Use aloop

This is more of a classic interview answer and reverses the string in a more manual way. All you need to do is loop through each character in the string, and reverse its order.

Take a look at the following code:

reverse = str => { let reversed = ""; for (let character of str) { reversed = character + reversed; } return reversed; };

All we are doing here is creating an empty reversed string in which we will push our characters.

Then we loop through the string we receive using an ES6 for...of loop ( see the docs ) and pushing each character to the reversed string.

So if we had hello then we would loop through and add h to a blank string. Then our next character is e which would be added to our string which has now become eh . Run in sequence you get:

This is pretty straightforward and has a time complexity of O(n).

Using Javascript helpers

Reduce is a Javascript array method which is really useful. I would use this in an interview to show that I’m aware of ES6 features, and it has the benefit of some quite compact syntax.

If you haven’t seen reduce() in action then it’s just a way to run a function on each item of the array and return a single value at the end. You can read a bit more in the docs .

As for a code example, it looks a little like this:

reverse = str => { return str.split("").reduce((rev, char) => char + rev, ""); };

So there are a few things going on here. Firstly, we convert our string to an array using split . Then we reduce each item of our array and in the callback we provide our accumulated results so far ( rev ), then we add the next character in the array to rev . We also shorten the syntax through arrow functions and an implicit return.

The logic is actually exactly the same as our loop above, but we use javascript to do some of the heavy lifting.

Conclusion

As you have seen above, it’s actually very simple to reverse a string once you have seen a few examples. Any of the above will accomplish your task though I recommend the last two for an interview.

Personally I prefer the second option as it shows your working a bit more than the last option which relies more on ES6 features.

Got another way to reverse a string? Tweet me! . Or try your hand at reversing strings on LeetCode .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:String Reversal in Javascript
本站链接:https://www.codesec.net/view/627916.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(18)