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.