Functions into Functions explainer

The aim of this puzzle: Use a different callback function in updateAllAndCheck()

Walkthrough of the solution: The checkPassport and updateAllAndCheck function definitions are already completed. The checkPassport function will take a string, check if it contains the string 'Passport' inside of it, and then prints whether whether it does or not. The updateAllAndCheck function runs a check on a string and then updates the string.

The updateAllAndCheck() function is called at the bottom of the code. Its arguments are travelDocument, 'grasshoper', 'grasshopper', and checkLength. The important part is the 4th argument, checkLength, which is the name of another function. That makes checkLength a callback function. The callback should be changed to checkPassport.

Sample code solution:
(Tap below to reveal)

function checkPassport(string) {
  string.includes('Passport') ? console.log('This document is a passport') : console.log('This document is not a passport');

function updateAllAndCheck(string, oldPart, newPart, extraCheck) {
  return updateAllNames(string, oldPart, newPart);

travelDocument = updateAllAndCheck(travelDocument, 'grasshoper', 'grasshopper', checkPassport);

JavaScript Concepts: Callback Functions, Calling Functions, Code Block (function), console.log()

Additional Code
// travelDocument values
let passport = `Passport...
Name: grasshoper,
Country: ${pickRandom(['Andorra','Barbados','Cameroon','Denmark','Eritrea','Finland','Guyana','Honduras','Israel','Japan','Kosovo','Lebanon','Malta','Nigeria','Oman','Philippines','Qatar','Rwanda','Slovenia','Togo','Uzbekistan','Vietnam','Yemen','Zambia'])}`;

let hotel = `Hotel Reservation for grasshoper...
Room ${pickRandom(499)+100}`;

let rental = `Vehicle Rental for grasshoper...
Type: ${pickRandom(['economy','mid-size','full-size','premium','luxury','minivan','convertible','SUV','van'])},
License: ${[...Array(6)].map(i=>(pickRandom(36)-1).toString(36)).join('').toUpperCase()}`;

let ticket = `Plane Ticket for grasshoper...
Flight GH${pickRandom(8999)+1000},
Depart: ${pickRandom(12)}:${pickRandom(49)+10},
Arrive: ${pickRandom(12)}:${pickRandom(49)+10}`;

let travelDocument = pickRandom([passport, hotel, rental, ticket]);

// Functions
function checkLength(string) {
  string.length > 20 ? console.log('Username too long') : console.log('Username updated');

function updateAllNames(string, oldPart, newPart) {
  if (string.includes(oldPart) === false) {
    return string;
  string = string.replace(oldPart, newPart);
  return updateAllNames(string, oldPart, newPart);

Completed the puzzle some how but don’t understand it.

I also completed by the instructions
but didn’t understand!

There are 2 functions created in the code: checkPassport and updateAllAndCheck. These don’t get edited, but it’s nice to see that they both look like normal functions.

One thing to notice about updateAllAndCheck: it has a parameter called extraCheck. Then we see that it uses that parameter as a function: extraCheck(string).

That means when we call the updateAllAndCheck(), we need to give it the name of another function as the 4th argument (since extraCheck is the 4th parameter).

When a function is created using function myFunction(...) {, there are 2 ways to use that function:
myFunction() – as a function call, when you want to run myFunction directly
myFunction – as a callback, when you want to use the function as a variable, specifically, as an argument for a another function.

In this puzzle, you want to change the callback function used in the updateAllAndCheck() function call. Instead of checkLength, it should use checkPassport.


1 Like

Correct me if i’m wrong, ExtraCheck is callback function without definition & we can updqte with other function what ever we like?

extraCheck is a parameter, just like string, oldPart, and newPart are parameters. Parameters are just temporary variables that are used inside of a function definition. The only difference for the extraCheck is that when you call the function, the argument for the extraCheck should be the name of a function (making it a callback function).


1 Like