0

I am trying to make a program that gets the name of a person and returns his initials e.g if his name is John Michael Rogers I would like to get as result JR(without the M). I have come up with a solution that only returns the last upper case letter. Here it is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Second task HS</title>
</head>

<body>

<form name="myForm" id="eForm">
    <label for="first">Name</label>
    <input type="text" id="name" name="fullname"><br>
    <input name="button" type="button" value="Pick" onclick="pick();"/>
</form>
<div id="result">
</div>

<script>
    var name = document.getElementById("name").value;
    function pick() {
        for(var i = 0; i < name.length; i++){
            if(name[i] === name[i].toUpperCase() && name[i] !== name[i].toLowerCase()){
                document.getElementById('result').innerHTML = name[i]
            }
        }
    }
</script>
</body>
</html>

How can I specify that i want the script to search for letters only from the first and last space separated parts of the string. Like in the example with John Michael Rogers, to search only in John and Rogers.

James
  • 2,195
  • 1
  • 19
  • 22
Henry Lynx
  • 1,099
  • 1
  • 19
  • 41
  • You're gonna want to get the element's value *inside* the `pick()` function. The way you have it, `name` will be set once the page loads, and will *never* be updated. – gen_Eric Jan 21 '14 at 16:29
  • possible duplicate of [Finding uppercase characters within a string](http://stackoverflow.com/questions/6055152/finding-uppercase-characters-within-a-string) – Liam Jan 21 '14 at 16:29
  • 1
    Things to consider. A person may not have a middle name, or they might have extended names like Mary Rose Smith Rogers. As far as a solution I would break the names into an array when you find a space. Then grab item[0] and item[2] in your array if you are sure you don't have any random exceptions. If you do then grab item[0] and the last item in the array say item[4] – Shawn Jan 21 '14 at 16:36

4 Answers4

1

Here'a a function that will take a name, split it up and returns an object that contains the capitalised letters of the first and final names, regardless of whether the name submitted is capitalised or not.

var name = 'John Michael Rogers';
var name2 = 'john Michael Rogers moore';

function getInitials(name) {
  var arr, nameArr, first, last;
  nameArr = name.split(' ');
  first = nameArr[0][0].toUpperCase();
  last = nameArr[nameArr.length - 1][0].toUpperCase();
  return {first: first, last: last};
}

getInitials(name); // { first: 'J', last: 'R' }
getInitials(name2); // { first: 'J', last: 'M' }

Demo

And here's a version that allow you to specify if you want the results returned as an object, array, or string:

Demo2.

Andy
  • 61,948
  • 13
  • 68
  • 95
1
function getInitials(fullName){
  var caps = fullName.replace(/[^A-Z]/g,'');
  if (caps.length>=2) return caps[0]+caps[caps.length-1];
}

var initials = getInitials("John Michael Caine Rogers");
//--> "JR"

getInitials("Aaron deChazal")     // "AC" (?)
getInitials("Jean-Paul François") // "JF" (?)
getInitials("Mary Smith-Jones")   // "MJ" (?)

This finds all capital letters, and then returns the first and last (with a test to return nothing if there are not two capital letters). As shown at the end, there are some edge cases you may want to consider what you really want.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
1

Here's a simple function that gets you initials, and you can specify whether you want to include last name initial or not:

function getInitials(fullName,includeLastName) {
    var initials = "";
    var nameArray = fullName.split(" ");
    if (nameArray.length===1 || (nameArray.length===2 && !includeLastName)) {
         initials=fullName.substr(0,1);
    } else {
            if (!includeLastName) {
                    nameArray.splice(-1,1);
            }
            for (var i=0; i<nameArray.length; i++) {
                    initials+=nameArray[i].substr(0,1);
            }
    }
    return initials.toUpperCase();
}
alert(getInitials("Kim Källström",true));         //=KK
alert(getInitials("Kim Mikael Källström",true));  //=KMK
alert(getInitials("Kim Mikael Källström",false)); //=KM
wanten
  • 184
  • 2
0

You can try with a regular expression for getting the first letter of the last word :

var str = "John Michael Rogers";
var patt = new RegExp(/.*\s(\w)\w*/g);
var lastFirstLetter = patt.exec(str)[1];
Ludo
  • 3
  • 3