Substrings In This Assignment You Will Use JavaScript To Ext
Substringsin This Assignment You Will Use Javascript To Extract Substr
Substrings in this assignment you will use JavaScript to extract substrings as described in chapter 16. Create a webpage that prompts the user for his/her name before the page loads. Using the substring method and the length property, find their first name. Using the indexOf method, find their last name. Using the charAt method, find their initials. Change their names to uppercase. Display the information you found in the body of your page, like this. (example of what I want is attached!!) Let me know if u have any question __ Due Tuesday, December 4th @ 9:30 AM
Paper For Above instruction
// Function to execute after the page loads
window.onload = function() {
// Prompt the user for their full name
let fullName = prompt("Please enter your full name (e.g., John Doe):");
// Validate input
if (fullName && fullName.trim() !== "") {
fullName = fullName.trim();
// Convert name to uppercase for display
let upperName = fullName.toUpperCase();
// Find the index of the space separating first and last name
let spaceIndex = fullName.indexOf(" ");
// Extract first name using substring and length
let firstName = "";
if (spaceIndex !== -1) {
firstName = fullName.substring(0, spaceIndex);
} else {
// If no space found, assume entire name is first name
firstName = fullName;
}
// Find last name using indexOf and substrings
let lastName = "";
if (spaceIndex !== -1) {
lastName = fullName.substring(spaceIndex + 1);
} else {
lastName = "";
}
// Find initials using charAt
let firstInitial = firstName.charAt(0);
let lastInitial = lastName.charAt(0);
// Convert initials to uppercase
firstInitial = firstInitial.toUpperCase();
lastInitial = lastInitial.toUpperCase();
// Prepare display content
const displayDiv = document.getElementById("result");
displayDiv.innerHTML = `
Full Name (Uppercase): ${upperName}
First Name: ${firstName}
Last Name: ${lastName}
Initials: ${firstInitial}${lastInitial}
`;
} else {
alert("No name entered. Please reload the page and enter your name.");
}
};