Transform learner name to proper case

Jun 07, 2023

I'm trying to write some JavaScript that will convert the learner's first name to proper case. I was able to successfully get it to convert to all uppercase using the following code:

var player = GetPlayer();
var newVar = player.GetVar("LastName");
newVar = newVar.toUpperCase();
player.SetVar("LastName", newVar);

But when I switched the trigger to a submit button, this no longer works. It used to trigger when the Textbox loses focus. I'm not sure why it doesn't work now that the trigger is different.

Here is the code I have which I think should convert it to proper case, but I'm not sure if the problem is the code or the submit button trigger.

var player = GetPlayer();
var myName = player.GetVar("FirstName");
var array = myName.split(',');
var str = array[1];
var newName = str.slice(0,1).toUpperCase() + str.slice(1);
player.SetVar("FirstName", newName);

Any help appreciated!

5 Replies
Crystal Ryan

Thanks! This worked for capitalizing the first letter, but didn't work if someone had for example: jAne or JANE. It would convert respectively to, JAne and JANE.

Do you have any ideas on how this could be modified to correct the name regardless of what combination of uppercase and lowercase has been used?

Thanks again for your response!

Crystal Ryan

I figured it out! I changed first and last name to lowercase first, before capitalizing the first letter. Note that this won't work for names like Ra'Chelle or McDonald, since it would convert them to Ra'chelle and Mcdonald, respectively. It's fine for my instance. In the future I might look into forums that talk about how to pull the learner name from the LMS the module is hosted in.

Here's the code in case anyone needs this same solution:

var player = GetPlayer();
var jsFirstName = player.GetVar("FirstName");
var jsLastName = player.GetVar("LastName");

var jsFirstName = jsFirstName.toLowerCase();
var jsLastName = jsLastName.toLowerCase();

var capitalizedFirstName = jsFirstName.charAt(0).toUpperCase() + jsFirstName.slice(1);
var capitalizedLastName = jsLastName.charAt(0).toUpperCase() + jsLastName.slice(1);

var fullName = capitalizedFirstName + " " + capitalizedLastName;

player.SetVar("Fullname", fullName); 

Richard Watson

Good to hear, Crystal. I think we had the same idea; I just executed slightly differently concerning the string manipulation. Both are valid and produce the result you want. I'm still learning JavaScript, so still in "learning mode".  I manipulated the code (not the one below) to get people with the last name McDonald to show correctly. Still, there are so many ways of capitalizing names that it might be a little cumbersome to figure out a way that works in every situation.  

Perhaps other people more knowledgeable in JavaScript can help you solve this, but for now, appreciate the chance to play around with the challenge you had.

Richard

=================

var player = GetPlayer();
var jsFname = player.GetVar("Fname");
var jsLname = player.GetVar("Lname");

var capitalizedFname = jsFname.charAt(0).toUpperCase() + jsFname.slice(1).toLowerCase();
var capitalizedLname = jsLname.charAt(0).toUpperCase() + jsLname.slice(1).toLowerCase();

var fullName = capitalizedFname + " " + capitalizedLname;

player.SetVar("Fullname", fullName);