Reputation: 421
I'm working on a challenge that takes in a string, then returns the string in all caps, a few replacements/substitutions for vowels, and has "!!!!" after each word.
function gordon(a){
return a.split(" ").map(function(x){return x.replace(/[aA]/g,"@").replace(/[aeiou]/g,"*") + "!!!! ";}).join("").toUpperCase();
}
This code works, and returns the right answer, except for ONE whitespace at the end of the last "!!!".
The main reason I'm asking this is because this is something that I feel like I run into a lot with the map method or for loops. What do you do if you want to affect all elements except the last one? Is there an easy way to accomplish this?
Upvotes: 1
Views: 7038
Reputation: 782066
Since you want space between the words after joining, put that in the .join()
call instead of after !!!!
.
function gordon(a){
return a.split(" ")
.map(function(x){
return x.replace(/[aA]/g,"@").replace(/[aeiou]/g,"*") + "!!!!";
})
.join(" ")
.toUpperCase();
}
The argument to .join()
is the separator put between each array element when they're concatenated into the result string.
Upvotes: 5