# Weird Weight Explainer

The aim of this puzzle:
Use `console.log()` to observe ow the code is running, and find the errors in the code.

Walkthrough of the solution:
The function `prepareRecipeCards()` runs three lines of code as the main program. The second line is `printTotalWeight()`. But the output doesn’t make sense:

``````> Total ingredient weight: 7two1.5 kilos
``````

To see what’s happening in the function, add two `console.log()` statements inside the for loop in `printTotalWeight()`.

At the top fo the code, two variables are imported on this line:

``````import {itemWeight, totalWeight} from 'debugging';
``````

`itemWeight` and `totalWeight` are helper strings to use for this puzzle.

First, at the top of the for loop, log `itemWeight + i.kilos`. Then at the bottom of the loop, log `totalWeight + total`. The final version of `printTotalWeight()` should look like this:

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

Now when the code is run, we see exactly what’s happening inside the loop. Here is the new output:

``````Item Weight: 4
> Total Weight: 4
> Item Weight: 2
> Total Weight: 6
> Item Weight: 1
> Total Weight: 7
> Item Weight: two
> Total Weight: 7two
> Item Weight: 1.5
> Total Weight: 7two1.5
> Total ingredient weight: 7two1.5 kilos
``````

Now we can see that the `kilos` property of one of the objects in the `ingredients` array is the string `two` instead of the number `2`.

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', kilos: 1.5};

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

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

function prepareRecipeCards() {
try {
printTotalWeight();
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',
{
}
);

defineModule(
'debugging',
{
itemWeight: 'Item Weight: ',
totalWeight: 'Total Weight: ',
}
);
``````
1 Like

Is there any reason why the first log of Item weight is before total += i.kilos?
At first I put both console logs together after total += i.kilos and what the android app shows in the console is the same as the example solution, yet the app needs me to put the first console log a line higher.
So, does it really matter? [quote=“grasshopperexplainer, post:1, topic:31654”]

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

[/quote]