Knowee
Questions
Features
Study Tools

Generate User NameFix getUserName so that it returns the user’s name as "LastName, FirstName" (without the quotes).Capitalise the first letter of the first and last namesIf the lastname is null, just return firstname without a commae.g. "John" "Smith" should return "Smith, John"

Question

Generate User NameFix getUserName so that it returns the user’s name as "LastName, FirstName" (without the quotes).Capitalise the first letter of the first and last namesIf the lastname is null, just return firstname without a commae.g. "John" "Smith" should return "Smith, John"

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide on how to fix the getUserName function in JavaScript:

  1. First, we need to check if the lastName is null. We can do this using an if statement.
if (lastName === null) {
    return firstName;
}
  1. If the lastName is not null, we need to return the user's name as "LastName, FirstName". We can do this using string concatenation or template literals. Also, we need to capitalize the first letter of the first and last names. We can do this using the charAt method and the toUpperCase method.
return lastName.charAt(0).toUpperCase() + lastName.slice(1) + ', ' + firstName.charAt(0).toUpperCase() + firstName.slice(1);
  1. Putting it all together, the getUserName function should look like this:
function getUserName(firstName, lastName) {
    if (lastName === null) {
        return firstName.charAt(0).toUpperCase() + firstName.slice(1);
    }
    return lastName.charAt(0).toUpperCase() + lastName.slice(1) + ', ' + firstName.charAt(0).toUpperCase() + firstName.slice(1);
}

This function will return the user's name as "LastName, FirstName" with the first letter of the first and last names capitalized. If the lastName is null, it will just return the firstName without a comma.

This problem has been solved

Similar Questions

For t5he above code please correct it as Generate User Name Fix getUserName so that it returns the user’s name as "LastName, FirstName" (without the quotes). Capitalise the first letter of the first and last names If the lastname is null, just return firstname without a comma e.g. "John" "Smith" should return "Smith, John"

Complete the program below. It should:define a function thatasks the user for their first nameasks the user for their last namereturns "x y", where x is their first name and y is their last nameoutputs the result of the function

package test3;public class Test3 { // DO NOT write a main method public static String getUserName (String firstName, String lastName) { // return the user name as LastName, FirstName e.g. Smith, John } … } }

Show first name and last name concatinated into one column to show their full name.

Write a function that reads your name in lowercase and perform the following operationprints it in uppercaseprints the length of the nameSample Inputsanjay kumarSample Outputramuk yajnas12

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.