newman newman - 24 days ago 5x
Javascript Question

Map method to effect all EXCEPT last array item

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?


Since you want space between the words after joining, put that in the .join() call instead of after !!!!.

function gordon(a){
    return a.split(" ")
            return x.replace(/[aA]/g,"@").replace(/[aeiou]/g,"*") + "!!!!";
        .join(" ")

The argument to .join() is the separator put between each array element when they're concatenated into the result string.