JavaScript first, middle, and last names

When working with Javascript, I have encountered a challenge. I am attempting to extract the First, Middle, and Last names from a full name input into three separate fields - Character Length, Middle Name, and Initials. At this point, I have successfully managed to determine the character length of the full name but extracting information between two spaces has proven to be a stumbling block.

// strings.js
// This script is designed to calculate the character length and initials of a given name.

// The function executes when the form is submitted.
// It calculates the values and returns false.
function formatNames() {
'use strict';

// Variables for storing Full Name, Middle Name, initials, and length:
var lengthName;
var middleName;
var yourInitials;


// Accessing the form value:
var fullName = document.getElementById('fullName').value;

// Calculate the character length:
lengthName = fullName.length;

// Extract the middle name:

// Display the character length:
document.getElementById('nameLength').value = lengthName;

// Prevent submission by returning false:
return false;

} // End of formatNames() function.

// This function adds an event listener to the form when the window loads.
function init() {
'use strict';
document.getElementById('theForm').onsubmit = formatNames;
} // End of init() function.
window.onload = init;

Answer №1

This assignment seems to focus on building your skills, so I'll provide some helpful hints without giving away all the answers.

If you utilize the split(' ') function with a string, it will generate an array of words separated by spaces.

By using the charAt(0) method on a string, you can extract the first letter from that string.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I attach a cookie to a div that becomes visible after a few seconds of video playback?

After 20 seconds of video play, a div element appears. I am trying to set up a cookie so that once the div is shown, it continues to appear unless the user clears their cache (cookie logic). This is my current attempt - http://jsfiddle.net/9L29o365/ Any ...

Error message: "Angular dependencies and provider not accessible"

I recently came across a file upload script on Github that I decided to implement. <script type="text/javascript" src="{% static 'bower_components/angular-file-upload/angular-file-upload.min.js' %}"></script> Furthermore, I have cre ...

employing this for the second parameter

Utilizing this.value for $(".selector") is my goal with $("#style_background") serving as my save button. However, when I implement this code, the value coming through is 'save'. How can I achieve this using dania and seablue? $("#style_backgrou ...

What is the best method for determining the central position of a .dae file and adjusting its placement?

As I work with numerous 3D models, I have noticed that many of them are not centered properly. Is there a method to determine the dimensions (length for x, width for z, height for y) of a model and divide it by two in order to accurately position the model ...

Creating Angular Modal on ng-click

I'm facing a challenge with a seemingly simple task. Within an ng-repeat, I have a table of events. My goal is to enable users to click on the event name and view detailed information in a modal window. To achieve this, I have set up an ng-click="ev ...

Phantom.js: Exploring the Power of setTimeout

Below is a snippet of code that intends for Phantom.js to load a page, click on a button, and then wait for 5 seconds before returning the HTML code of the page. Issue: Utilizing setTimeout() to introduce a delay of 5 seconds leads to the page.evaluate fu ...

JavaScript game with server-side communication and answer validation functionality

In my fast-paced, quiz-like Javascript game, users must answer a series of Yes/No questions as quickly as possible. Upon answering, the response is sent to the server for validation and feedback (correct/incorrect) before moving on to the next question usi ...

Display only the offcanvas button

Having trouble with Bootstrap 5 offcanvas? The offcanvas doesn't hide when I click the button again. <button data-bs-toggle="offcanvas" role="button">Add to Cart</button> Every time I click the button again, the offcan ...

Single array returned by observable

Issue: I am looking for a way to consolidate the multiple arrays returned individually into a single array. Solution: fetchAllRiders() { var distanceObs = Observable.create(observer => { this.http.get(this.API + '/driver/all').map(res = ...

Creating dynamic images in JavaScript by assigning URL paths from string variables

Currently, I am utilizing JavaScript to parse through an XML file. One interesting aspect of the XML is that it contains URLs which link to images like this one: http://localhost/pic.jpg Throughout the parsing process, I am storing each URL as a string va ...

AngularJS - displaying a notification when the array length is empty and customizing the visibility to conceal the default action

I've been working on an Angular project where I display a loading circle that disappears once the content is loaded. This circle is simply a CSS class that I call from my HTML only when the page first loads, like this: Actually, the circle consists o ...

How come my reducer isn't changing the state of my store with ImmutableJS?

I have the following code within my reducer: const copy_states = fromJS(state); const i_copy_jobs = copy_states.get('calendar_jobs').get(s_from_day_key).get(s_dept_id).get(s_job_id); let i_calend ...

Are there any testing frameworks available for JavaScript that are similar to Junit and specifically designed for testing object-oriented JavaScript within Node

What are some recommended methods for testing object-oriented JavaScript in Node.js? For instance, let's consider the following Cat.js class: function Cat(age, name) { this.name = name || null; this.age = age || null; } Cat.prototype.getAge ...

Innovative approach to rendering a popup component in Vue.js

I am in the process of developing a VueJS application and facing a particular challenge. I want to show different popups when right-clicking on various components within the app. My main concern is that each component should have its own unique popup. I ha ...

I'm having trouble pulling images from Firebase into my Vue application

Currently, I am experimenting with a Vue application that requires users to upload an image to Firebase. After the image is successfully uploaded, it should be displayed within an img tag in the template. The issue I am encountering is retrieving the uplo ...

What is the best way to pass parameters in jQuery using .on()?

Hello there, I have a slight dilemma :) Could you please advise me on how to pass a parameter to an external JavaScript function using the .on method? Here is the code snippet: <script> var attachedPo = 0; $this.ready(function(){ $ ...

Filtering out duplicate names from an array of objects and then grouping them with separator IDs can be achieved using JavaScript

Imagine we have an array [ {id: 1, label: "Apple"}, {id: 2, label: "Orange"}, {id: 3, label: "Apple"}, {id: 4, label: "Banana"}, {id: 5, label: "Apple"} ] We want the output to be like this [ {id: 1~3~5, l ...

inside the connect module, the res._renderHeaders function is located

Currently, I am delving into the patch.js file within the connect module. In it, I found some intriguing code snippets: var http = require('http') , res = http.ServerResponse.prototype , setHeader = res.setHeader , _renderHeaders = res._re ...

How to modify the overlay color in the TouchableHighlight component using an arrow function in React Native

With touchableHighlight, I found that I could easily modify the overlay color using the following code: <TouchableHighlight onPress={this.toggle.bind(this)} underlayColor="#f1f1f1"> However, when attemptin ...

What is the standard practice in AJAX for determining a specific state during page loading?

Since I started diving into the world of serious ajax operations, one question has been on my mind. Let me illustrate with an example. Imagine fetching a standard HTML page for a customer from the server. The URL could look like this: /myapp/customer/54 ...