Challenge: Check the Names

This challenge will test your skills in using callback functions to implement error checks in JavaScript.

Problem statement #

This exercise is to help you implement callback functions. In this challenge, assume you have a form that requires a person’s first name and last name. However, the first name is compulsory and cannot be skipped, whereas the last name is optional. Your task is to implement the function checkName, which checks for the first and last name of a person.

  • Function should throw an Error, No First Name Entered, if the first name is missing

  • Function should return the first name even if the last name is missing

  • Function should return the full name if both the first name and last name are given

Note: You need to implement the checkName function using the callback function.

Input #

checkName called with the first name, last name, and the callback function

Output #

Display either the first name, full name, or error

Sample input #

checkName("John" , "Doe" , callback(arg))

Sample output #

JohnDoe

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.