Fname
Jump to navigation
Jump to search
function formatName(fullName) {
const nameParts = fullName.split(' '); // Split the full name into an array of parts
if (nameParts.length >= 2) { // Ensure there are at least two parts (first and last name)
const firstName = nameParts[0];
const lastName = nameParts[nameParts.length - 1]; // Get the last part as the last name
return `${lastName}, ${firstName}`; // Construct the new format
} else {
return fullName; // Return the original name if it doesn't fit the expected format
}
}
// Example usage in HTML: const originalName = "John Doe"; const formattedName = formatName(originalName);
// Display the formatted name in an HTML element document.getElementById('nameDisplay').textContent = formattedName;
// HTML structure example:
//