3. Use indexOf0 and substr0 or substring0 to divide your name into two strings-f
ID: 3914089 • Letter: 3
Question
3. Use indexOf0 and substr0 or substring0 to divide your name into two strings-firstname and lastname We're going to use some basic built-in string functions to manipulate a string: your name. Modify the code in the js/hw2SplitString.js file to output the first and last name separately into the page below. Your code should work for any name entered. See the JS file for more on what input validation is expected is my full name. Split my name! Placeholder first name is my first name Placeholder last name is my last nameExplanation / Answer
Please replace your code below comment block with the following lines. It should work. Ensure the ids of elements are given correctly ... Let me know through comments if it still does not work, I'll help. Please do rate the answer if it helped. thank you.
var index = fullname.indexOf(" ")
var firstname = "", lastname = "";
if(index == -1)
firstname = fullname;
else
{
firstname = fullname.substring(0, index);
lastname = fullname.substring(index+1);
}
document.getElementById("firstname").innerHTML = firstname;
document.getElementById("lastname").innerHTML = lastname;
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.