Home > other >  Replace name and return an object
Replace name and return an object

Time:09-07

We need to keep track of the school students, but the data we currently have combines the students first and last names into a single name. You have been asked to separate the names to make the data easier to work with.

The makeStudentList function takes an object with a name property whose value will be a string consisting of a first name and a last name, separated by a space. The function should return an object.

The function should remove the name property, replace it with firstName and lastName properties, as shown in the examples below.

Examples:

makeStudentList({ name: "Hannah Fry", age: 4 }) // should return { firstName: "Hannah", lastName: "Fry", age: 4 }

makeGuestList({ name: "Paul Erdős", age: 6 }) // should return { firstName: "Paul", lastName: "Erdős", age: 6 }

CodePudding user response:

def makeStudentList(name,age):
    Name=name.split(" ")
    return {"FirstName":Name[0],"lastName":Name[1],"Age":age}
    
        
makeStudentList("Hannah Fry",4)

Here you go, lemme know if you need any sort of explanation, cheers:)

CodePudding user response:

function makeStudentList(student) {
    const [firstName, lastName] = student.name.split(" ");
  delete student.name;
  student.firstName = firstName;
  student.lastName = lastName;
  return student;
} // should return { firstName: "Hannah", lastName: "Fry", age: 4 }

let response = makeStudentList({ name: "Hannah Fry", age: 4 });

const p = document.querySelector('p');
p.append(JSON.stringify(response));
<p></p>

  • Related