<h4>3. Use indexOf() and substr() or substring() to divide your name into
two strings—firstname and lastname</h4>
<p>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
https://jsfiddle.net/jordany2017/ofwpck1L/1/#update is expected.
</p>
<p><input size="30" id="fullName"> is my full name. </p>
<button id="splitName" >Split my name!</button>
<br>
<span class="" id="Jordan"></span> is my first name<br>
<span class="" id="Marechal"></span> is my last name<br>
JavaScript
var splitName = document.getElementById("splitName");
splitName.onclick = function(){
var fullname = document.getElementById(" ").value;
/* fullName.indexOf(‘J’); // 0
// fullName.indexOf(‘n’); // 1
// fullName.indexOf(‘ ‘); // 5
var fullname = ‘Jordan Marechal’;
firstname.indexOf(‘Jordan’); // 4
var fullname = ‘Jordan Marechal’;
firstname.indexOf(‘J’); // 1
language.indexOf(‘n’, 6); // 3
var fullname = ‘Jordan Marechal’;
lastname.indexOf(‘Marechal’); // -1
var fullName = ‘Jordan Marechal’;
fullName.indexOf(‘n’); // 1
fullname.slice(6);
fullname.slice(0,8);**/
/*
* We've gotten the fullname from the HTML form field.
* Your job is to use the String.slice(), String.substring() or String.substr() functions
* to divide your name into separate first and last name strings and assign
* them to the variables provided. You may
* need String.indexOf() as well.
*
* You may not hardcode the position of where to split the string. Your code should
* work for anyone's first and last name. If the user enters a name without any whitespace
* return that as firstname and leave lastname blank.
*
* If a name has more than one whitespace (as in, using one or more middle names),
* make the first name 'firstname' and assign the rest to 'lastname'
*
**/
var firstname = '';
firstname.split ("");
var lastname = '';
lastname.split("");
// var res = "Marechal";
// var res = str.split (" ");
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.