Search⌘ K

Solution Review: Check the Names

Understand how to implement asynchronous callback functions that validate first and last names. Learn to handle errors when names are missing and how to return combined names using callbacks in JavaScript.

We'll cover the following...

Solution #

Javascript (babel-node)
const checkName = (firstName, lastName, callback) => {
if (!firstName) return callback(new Error('No First Name Entered'))
if(!lastName) return callback(firstName)
const fullName = `${firstName}` + `${lastName}`
return callback(fullName)
}
function callback(arg){
console.log(arg)
}

Explanation #

We need to cater to three conditions:

  1. If the first name is not given

  2. If the last name is not given

  3. If both ...