List Lacks Leeks Explainer

The aim of this puzzle:

Use a try-catch statement to catch an error in pre-defined code.

Walkthrough of the solution:

The starter code defines some data and several functions. But nothing runs, so add a function call to prepareRecipeCards() at the end of the code.

The code runs now, but there is an error. If a try…catch statement is added inside the prepareRecipeCards(), the code can handle whatever error is occuring.

At the top of the prepareRecipeCards(), add a try…catch statement. Inside the try block, put all the functions that are inside the function. Delete them form the function. It should look like this:

 try {
    addIngredient(leeks);
    printTotalWeight();
    ingredients.forEach(printIngredient());
  } catch (e) {
  }

The catch block catches the error, but still nothing is printed out. The last step is to add console.log(e) to the catch block. When the code runs now it will print the error.

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: 'two',
  },
];

let leeks = {name: 'leeks', weight: 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();
    ingredients.forEach(printIngredient());
  } catch (e) {
    console.log(e);
  }
}

prepareRecipeCards();

JavaScript Concepts: number, string, try...catch block

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

Can someone help me with the list lacks leeks ?

Can someone help me on with the list lacks leeks?


Solution for List Lacks Leeks

![bad code|690x388](upload://lY90EbG6vHQ7QAzpygPEzZJIHNy.pn
in the code, list Lacks Leeks my code has the same answer but it says to do Try moving the functions that were inside the prepareRecipeCards() function into the try block. but the functions are in the right place

1 Like

Blockquote

1 Like

Hello @Grasshopper_Eric, Actually I wanted Ben to be here But i couldn’t mention him, So i mentioned you. The problem is I can’t do This puzzle “List Lacks Leeks”. The Code is Correct but does not work, Does Keeping saying “Try Deleting the three functions inside of prepareRecipeCards() and reinserting them into the try { } block”, It’s in the Correct place but does not work. I need a solution this is the last module, All Others have been done. Please Help me.

Sincerely.
-Krish

1 Like

@KrishIsUltraCool I also got the same problem then I created another one “try catch” box below the previous. After running it says try removing the code outside the “try catch” box. And I removed upper “try catch” box and it worked.

@ganesh_katare It’s doesn’t work, Can you send the Screenshort of the code?
It Maybe can help me. Thanks for the advice.

Soo, Do you know the answer of “List Lacks Leeks”?
Provide me if you have it. I don’t know, Why it does this

@GameskillsYT007 You Forgot to add “leeks” Thing in try { } block in addIngredient()
The try…catch… block should be like this

try {
addIngredient(leeks,_ _ );
printTotalWeight();
ingredient.forEach(printIngredient(
_ _));
} catch ( e ) {
console.log(e);

@KrishIsUltraCool Now I did different, you can try may it works for you

First do like this

Run the code …
Then change printIngredient with printIngredient()

The First Answer You said to do didn’t work, Second one, I Couldn’t Find printIngredient, there was only printIngredient(), I Can’t do it still, I also contacted the support and i’m waiting for a respond. Thanks again for the Advice. :slightly_smiling_face:

Regards
Krish.

@KrishIsUltraCool at the bottom create variable as let printIngredient = 0.
Then replace it with printIngredient().
Then delete ‘let printIngredient=0’.
Run the code
It will ger error
Then lastly change printIngredient with printIngredient() and run the code.

Hey, I finally got help from Joshua, Maybe He is a support team member. My mistake was I didn’t correctly wrote the code, He said that i need to change “ingredient” to “ingredients”.
Sorry for disturbing you for a long time. :pensive:. I hope you will forgive me. I was so careless about it. :pensive:

Can someone tell me what’s wrong?
It keeps telling me to move the functions to the try blocks

here’s the solution-


@Spinestdog816

1 Like

i cant see your list of food
@Spinestdog816