Printing Problem Explainer

The aim of this puzzle:
Read an error message printed from the catch block, and fix the error.

Walkthrough of the solution:
The try…catch statement added in List Lack Leeks is catching an error and printing it out. Here is the error message:

> Cannot read property 'kilos' of undefined

In the previous puzles List Lacks Leeks and Weird Weight, we fixed some errors. So take a look at the final line in the try block:

ingredients.forEach(printIngredient());

Take a look at the definition of the function printIngredient():

function printIngredient(ingredient) {
  print(ingredient.kilos + ' kilos ' + ingredient.name);
}

The forEach function expects an argument of a callback function. But notice that instead of passing a callback function, the code actually runs printIngredient() and passes the result to forEach(). printIngredient() expects an agument, but none is provied. That is why the code creates an error. If the parameter ingredient is undefined, the code cannot access the property kilos.

Simply remove the function call and replace it with the callback function printIngredient. Notice that there are no parentheses this time. Now instead of running the function printIngredient() once with no arguments, it will run for each element of ingredients, passing the element as the argument. Now the code can find the properties it expects.

Sample code solution:
(Tap below to reveal)

import {addIngredient} from 'cookbook';
import {itemWeight, totalWeight} from 'debugging';

let ingredients = [
  {
    name: 'potatoes',
    kilos: 4,
  },
  {
    name: 'carrots',
    kilos: 2,
  },
  {
    name: 'onions',
    kilos: 1,
  },
  {
    name: 'chickens',
    kilos: 2,
  },
];

let leeks = {name: 'leeks', kilos: 1.5};

function printTotalWeight() {
  let total = 0;
  for (let i of ingredients) {
    total += i.kilos;
  }
  print('Total ingredient weight: ' + total + ' kilos');
}

function printIngredient(ingredient) {
  print(ingredient.kilos + ' kilos ' + ingredient.name);
}

function prepareRecipeCards() {
  try {
    addIngredient(leeks);
    printTotalWeight();
    print('Ingredients:');
    ingredients.forEach(printIngredient);
  } catch (e) {
    console.log(e);
  }
}

prepareRecipeCards();

Additional code Additional Code (hidden code that runs before the puzzle’s code):

function _addIngredient({name, kilos}) {
  if (isNaN(kilos)) {
    throw new Error(`kilos must be a number. got ${kilos} instead`);
  }
  ingredients.push({
    name,
    kilos,
  });
  console.log(`Added ${name}, ${kilos} kilos, to the ingredients list.`);
}

defineModule(
    'cookbook',
    {
      addIngredient: _addIngredient,
    }
);

defineModule(
    'debugging',
    {
      itemWeight: 'Item Weight: ',
      totalWeight: 'Total Weight: ',
    }
);
1 Like
type or paste cofunction prepareRecipeCards(___) {
  try {
    addIngredient(leeks, ___);
    printTotalWeight();
    ingredients.forEach(printIngredient);
    ___
  } catch (e) {
    console.log(e);
  }
};
prepareRecipeCards();
de here
1 Like

Just change printIngredient () inside of Ingredient.for Each to, (printIngredient ).

1 Like