Reputation: 73
In Javascript, I am trying to take a full name (First, Middle, and Last) that is typed into a field and once the submit button is clicked to outputs to three separate fields Character Length, Middle Name, and the 3 initials. I have got to the character field so far but I am stumped on how to take the information that is between two spaces.
// strings.js
// This script takes a name and spits out its character length with initials.
// Function called when the form is submitted.
// Function performs the calculation and returns false.
function formatNames() {
'use strict';
// For storing the Full Name, Middle name, initials, length:
var lengthName;
var middleName;
var yourInitials;
// Get a reference to the form values:
var fullName = document.getElementById('fullName').value;
// Perform the character spit out:
lengthName = fullName.length;
// Pull out the middle name:
// Display the volume:
document.getElementById('nameLength').value = lengthName;
// Return false to prevent submission:
return false;
} // End of formatNames() function.
// Function called when the window has been loaded.
// Function needs to add an event listener to the form.
function init() {
'use strict';
document.getElementById('theForm').onsubmit = formatNames;
} // End of init() function.
window.onload = init;
Upvotes: 1
Views: 4690
Reputation: 46647
This sounds homework-ish so I'll give you some tips without spoiling the learning experience.
You can use split(' ')
on a string to get an array of words (seperated by spaces) in that string.
You can use charAt(0)
on a string to get the first letter of a string.
Upvotes: 1