Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Abbreviation of city names?

Avatar

Level 2

Good Morning, I have found from this site the coding needed to pull the initials of any given name with two words.

ie. from the word Nova Scotia, the code below pulls out the initials "NS" into the nameabr text field when placed on the exit event of the name field in Javascript.

var name = this.rawValue

var initials = "";

var wordArray = name.split(" ");

for(var i=0;i<wordArray.length;i++)

{

    initials += wordArray[i].substring(0,1);

}

Nameabr.rawValue = initials;

now...I would like to change this code a bit so that if there are instances where a name is one word, for example "Alberta", the script would then pull out the first three letters of the word ie. "Alb" rather than just resulting in "A". But I would like to keep the initials if the name is more than one word, ie. revert to the above code.

I assume that there would initially be a count of word

Thanks ahead of time.

J

1 Accepted Solution

Avatar

Correct answer by
Level 8

BEFORE the for statement.

var name = this.rawValue
var initials = "";
var wordArray = name.split(" ");

if (wordArray.length==1) 

initials=wordArray[0].substring(0,3);

else
for(var i=0;i<wordArray.length;i++)
{

initials += wordArray[i].substring(0,1);
}
Townabr.rawValue = initials;

Kyle

View solution in original post

4 Replies

Avatar

Level 8

You can put:

if (wordArray.length==1)

initials=wordArray[0].substring(0,3);

else

right before your for statement.

Also, if you are using this for the purpose of changing province names into short form, I would recommend using a list with the official province codes (or you might have just been using that as an example).

Kyle

Avatar

Level 2

Hi dcidev,

Like so? I tried below and the townabr field did not update.

var name = this.rawValue
var initials = "";
var wordArray = name.split(" ");
for(var i=0;i<wordArray.length;i++)
{
    if (wordArray.length==1)

    initials=wordArray[0].substring(0,3);

    else

    initials += wordArray[i].substring(0,1);
}
Townabr.rawValue = initials;

J

Avatar

Correct answer by
Level 8

BEFORE the for statement.

var name = this.rawValue
var initials = "";
var wordArray = name.split(" ");

if (wordArray.length==1) 

initials=wordArray[0].substring(0,3);

else
for(var i=0;i<wordArray.length;i++)
{

initials += wordArray[i].substring(0,1);
}
Townabr.rawValue = initials;

Kyle

Avatar

Level 2

Thank you so much Kyle, I had inadvertently placed this code in the change event and not the exit event at first, but after placing in the right event, it worked!!! amazing!

J