Get Initials

Function, that returns the initials of a given name ( the first letters of first and last names )

by Ico Dimchev

HTML

<div id="initials"></div>

JavaScript

/*
	Функция, която ввръща инициалите от дадено име fullname
*/
var $initials = document.getElementById("initials");
var fullName = "христо д. панайотов";
getInitials(fullName);

function getInitials(_name) {
	var names = _name.split(" ");
	var initials = "";
	if (names.length < 2) {
		throw "Моля, въведете поне две имена!";
	} else {
		initials = String(names[0][0] + ". " + names[names.length - 1][0] + ".").toUpperCase();
		$initials.innerHTML = fullName + " инициали: " + "<b>" + initials + "</b>";
		return initials;
	}
}