Basic JavaScript Algorithms: Reversing Word Order
In an effort to help new devs and to memorize what I've learned in a year or so since starting to learn JavaScript, I'll be posting and describing some methods that I've come across in coding challenges and project work.
This article assumes that you, the reader, are familiar with the following:
- [x] functions
- [x] arrays
- [x] strings
- [x] return statements
In this first challenge, we are given instructions to complete a solution so that it reverses all of the words within the string passed into the function's parameters.
function reverseWords(str){
return str;
If we look at the test cases provided, we are given the string and the resulting string that has gone through the function. This is helpful because it leaves less to interpretation. In this case, we have
Test.assertEquals(reverseWords("hello world!"), "world! hello")
This tells us that we are actually not reversing the characters in the string but we are reversing the order of the words in the string.
Going back to the function:
function reverseWords(str){
return str;
We will be using the following built-in methods:
.split()
.reverse()
.join()
First, we want to convert the string into an array with the .split()
method. We can call the methods directly on the return statement.
function reverseWords(str){
return str.split(" ");
With str
as our input parameter representing our test string, "Hello World!", calling the .split()
method outputs ['hello', 'world!']
- effectively transforming our string into an array of substrings. Because we've assigned an empty space between the quotation marks, we are able to manipulate the space between the words in the original string. In short, the .split()
method allows us to form a new array of strings based on the parameter (โ โ)
.
We're not done yet. We still need to reverse the order of the words!
By chaining the .reverse()
method onto our return statement like:
function reverseWords(str){
return str.split(" ").reverse();
we get...
['world!', 'hello']
Awesome, but no cigar just yet. The words are in an array, and we need them to be a single string. Have no fear the .join()
method is here!
function reverseWords(str){
return str.split(" ").reverse().join(" "); }
By chaining the .join(" ")
method with a space between the quotation marks to indicate where we want to join the elements (between the spaces), we are able to convert the reversed word array of strings back into a singular string which outputs as:
'world! hello'
That concludes this review! Thanks for reading! Keep up the great work because, as Missy would say, "It's your fringa-ninga-nam-yamtham!" ๐
References
- developer.mozilla.org/en-US/docs/Web/JavaSc..
- developer.mozilla.org/en-US/docs/Web/JavaSc..
- developer.mozilla.org/en-US/docs/Web/JavaSc..
- developer.mozilla.org/en-US/docs/Web/JavaSc..
- developer.mozilla.org/en-US/docs/Web/JavaSc..
- developer.mozilla.org/en-US/docs/Web/JavaSc..
- developer.mozilla.org/en-US/docs/Web/JavaSc..
Cover Image
- Missy "Misdemeanor" Elliot via wardcertified.com/blog/2020/3/10/flip-it-an..