The slice() method returns an empty string in this case. There are a couple of differences between the String.substring() and the String.slice() methods: The substring() method swaps its start and end index if the start index is greater than the end index. You may need to turn the string into an array before you can reverse it. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. However, notice that we didn't use a negative end index in the call to String.substring(). Here's an Interactive Scrim Showing How to Reverse a String in JavaScript Algorithm Challenge Reverse the provided string. Note that this article is based on the freeCodeCamp Basic Algorithm Scripting “ Reverse a String”. There are potentially tens of different ways to do it, excluding the built-in reverse function, as JavaScript does not have one.īelow are my three most interesting ways to solve the problem of reversing a string in JavaScript. Interviewers may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or they may even ask you to reverse a string using recursion. Reversing a string is one of the most frequently asked JavaScript question in the technical round of interview.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |