I'd like to divide the elements of the array into individual strings, then apply numeric values to each, and ultimately deliver the

I need help splitting strings in an array and rearranging them so the last name comes first followed by a comma. For example, "Stephen Curry" should become "Curry, Stephen."

This task is really confusing me. I've attempted it multiple times without success. :(

function rearrangeNames(){

var warriorsArray = new Array(5);
warriorsArray[0] = "Stephen Curry";
warriorsArray[1] = "Andre Iguodala";
warriorsArray[2] = "Klay Thompson";
warriorsArray[3] = "Andrew Bogut";
warriorsArray[4] = "David Lee";

var newName = warriorsArray[0].split(' ');
return newName[1] + ', ' + newName[0];
}

rearrangeNames();

Answer №1

Experiment with combining splitting, reversing, and merging methods.

let piratesArray = new Array(5);
    piratesArray[0] = "Captain Jack Sparrow";
    piratesArray[1] = "Blackbeard";
    piratesArray[2] = "Calico Jack";
    piratesArray[3] = "Anne Bonny";
    piratesArray[4] = "Bartholomew Roberts";

for (let i = 0; i < piratesArray.length; i++)
    piratesArray[i] = piratesArray[i].split(' ').reverse().join(', ');

console.log(piratesArray);
//["Sparrow, Captain Jack", "Blackbeard", "Jack, Calico", "Bonny, Anne", "Roberts, Bartholomew"]

Answer №2

function rearrangePlayerNames(){
    var warriorsArray = new Array(5);
    warriorsArray[0] = "Stephen Curry";
    warriorsArray[1] = "Andre Iguodala";
    warriorsArray[2] = "Klay Thompson";
    warriorsArray[3] = "Andrew Bogut";
    warriorsArray[4] = "David Lee";

    modifyAndDisplay(warriorsArray);
}

function modifyAndDisplay(arr){
    for(var i=0; i<arr.length;i++){
        var words = arr[i].split(" ");
        words.reverse();
       arr[i] = words.join(", ");
   }
   console.log(arr);
};

rearrangePlayerNames();

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

The latest bug fix and update in Bootstrap 4.5.2 now includes an updated version of Popper.js. Make sure to use the

Hello fellow developers, I am currently working with npm bootstrap version 4.5.2 and above. However, I am facing an issue with the compatibility of @popperjs/core. If anyone can assist me in resolving the bootstrap.js error temporarily, specifically re ...

What is the functionality of the filterBy method in Vue.js 1.x?

Looking to create a filter for my app similar to the filterBY Vue.js feature on older versions... Currently working on a Computed Property to generate an array containing objects that match a specified string variable... However, my goal is to create a re ...

Incorporating Angular, Angular UI, and Typescript while using the "controller as" approach

I'm having trouble with the combination in the topic, there must be a minor mistake somewhere that's causing this issue. Controller: class JobCtrl { job: Object; public $inject = ['$log', '$resource', &apos ...

In the world of GramJS, Connection is designed to be a class, not just another instance

When attempting to initialize a connection to Telegram using the GramJS library in my service, I encountered an error: [2024-04-19 15:10:02] (node:11888) UnhandledPromiseRejectionWarning: Error: Connection should be a class not an instance at new Teleg ...

a guide to displaying the contents of a 1-dimensional array within a square of size N x N

Creating a box using a 1D array is my current challenge, as I am not allowed to use a 2D array. Let's take a look at the code I have so far: String input = ""; int N = InputNumber(input); char [] Board = new char[N]; int M = (int) Math. ...

I am encountering an issue with my req.params.id being undefined. What steps should I take to troubleshoot and correct my

I am currently testing the function getIndividualTwitt using Postman and encountering an issue where I am getting 'undefined' when I console.log the req.params.id. Interestingly, this problem is only occurring within this specific route. Any assi ...

Error message: TypeError - AngularJS is not a function

Currently, I am attempting to integrate jQuery Geocomplete into my AngularJS application. This is the section of code in my HTML file: <input id="destination-location-input" type="text" class="form-control" ng-model="destCtrl.destination" ng- ...

Interactive grid feature created with HTML Canvas

Imagine my surprise when I discovered that the latest version of Google Spreadsheets is now using a canvas tag to render the spreadsheet grid, unlike the traditional <table><tr><td> method used in the past. In the previous version, only ...

what is the process for accessing data array data provider in yii2 framework?

I have successfully implemented ArrayDataProvider in my Yii2 controller. <?php public function actionPagination_product2(){ $tb_tab=Tablet::tableName(); Digital.inner_memory')->asArray()->all(); $data = Digital::find()->joinWith ...

Executing code within Vue js $set method's callback

For my v-for loop that generates a list of flights, I have implemented functionality where the user can click on a "flight details" button to send an AJAX request to the server and append new information to the results array. To update the view accordingly ...

Prevent form submission using jQuery based on ajax response, or allow it to submit otherwise

After setting up some jQuery code to handle form submission, the functionality is working well when certain conditions are met. However, I am facing a challenge in allowing the form to be submitted if the response received does not match specific criteria. ...

execute action once element has finished loading - angular

I am looking for a way to trigger an angular function after a specific element has been displayed on the page. The challenge arises because this element is part of a Single Page Application (SPA) where its display is controlled by a series of events. Tradi ...

How can you transfer array elements to a new array using JavaScript?

I have a task to transform the fields of an array received from one server so that they can be understood by another server for upload. My approach involves first retrieving and displaying the original field names from the initial server's array to al ...

Update the style of the legend from bars to a line chart in chart.js version 2.4.0

https://i.sstatic.net/BXcXj.pngI'm currently using chart.js version 2.4.0 for displaying graphs, and I'm having trouble changing the legend style from bar to line. Is there a way to switch the legend display from bar to line in chart.js? Here i ...

A guide on utilizing Java's Collections to organize a list filled with objects from a class

Struggling to figure out how to properly sort this list. When I attempt to use Collections.sort(mylist);, an error surfaces that is beyond my comprehension due to my beginner status in Java. Myclass x1 = new Myclass("8", "12"); Myclass x2 = new Myclass("6 ...

Is it possible to disable a function by clicking on it?

I currently have a website that is utilizing the following plugin: This plugin enables an image to be zoomed in and out through CSS transforms, with default controls for zooming in and out. My goal is to incorporate a reset button that returns the image ...

Struggling to retrieve the value in Node.js

Currently, I am in the process of developing a Node.js script to perform the following tasks: Read a file line by line Identify a regex match and store it in a variable Utilize this value in subsequent operations Below is the code snippet that I have im ...

JavaScript Recursive Function for Generating FancyTree JSON Structure

I'm currently building an array of JSON objects for FancyTree (https://github.com/mar10/fancytree/wiki/TutorialLoadData). My JSON source is from the Jira REST service, and its structure can vary widely. Therefore, the code to construct the array of JS ...

Is it time to set up your own MySQL Database?

let connection = mysql.createConnection({ user: 'root', password: '1234', database: 'data101', port: 3306 }); While using the MySQL package for NodeJS to create a database, I have a question. Do I need to manually cr ...

Check to see if the menu item exceeds the allotted space, and if so, display the mobile menu

Currently, I am in the process of creating a unique responsive menu for my website that I think will need some JavaScript implementation. My skills with JavaScript are not very strong, so I am looking for guidance, code examples, or resources that can assi ...