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"
Solution
Here is a step-by-step guide on how to fix the getUserName function in JavaScript:
- First, we need to check if the
lastNameisnull. We can do this using anifstatement.
if (lastName === null) {
return firstName;
}
- If the
lastNameis notnull, 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 thecharAtmethod and thetoUpperCasemethod.
return lastName.charAt(0).toUpperCase() + lastName.slice(1) + ', ' + firstName.charAt(0).toUpperCase() + firstName.slice(1);
- Putting it all together, the
getUserNamefunction 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.
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
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.