In the first iteration, item
is { type: "shirt", price: 4000 }
, so item.type
is "shirt"
. This also means the reference totals[item.type]
(total["shirts"]
will be undefined
since totals
starts as an empty object with no properties. If you try to add 1
to undefined
, this evaluates to NaN
which is “falsy”. The ||
operator between two operands will cause the operand that is “truthy” (the 1
on the right side) to be assigned to totals[item.type]
(currently totals["shirt"]
. This means after this iteration of the reducer, totals
looks like { shirt: 1 }
.
In the second iteration, item
is { type: "pants", price: 4532 }
, so item.type
is "pants"
. This also means the reference totals[item.type]
(total["pants"]
will be undefined
since totals
starts as an empty object with no properties. If you try to add 1
to undefined
, this evaluates to NaN
which is “falsy”. The ||
operator between two operands will cause the operand that is “truthy” (the 1
on the right side) to be assigned to totals[item.type]
(currently totals["pants"]
. This means after this iteration of the reducer, totals
looks like { shirt: 1 , pants: 1 }
.
In the third iteration, item
is { type: "socks", price: 234 }
, so item.type
is "socks"
. This also means the reference totals[item.type]
(total["socks"]
will be undefined
since totals
starts as an empty object with no properties. If you try to add 1
to undefined
, this evaluates to NaN
which is “falsy”. The ||
operator between two operands will cause the operand that is “truthy” (the 1
on the right side) to be assigned to totals[item.type]
(currently totals["socks"]
. This means after this iteration of the reducer, totals
looks like { shirt: 1 , pants: 1, socks: 1 }
.
In the fourth iteration, item
is { type: "shirt", price: 2343 }
, so item.type
is "shirt"
. This also means the reference totals[item.type]
(total["shirts"]
will be 2
since totals["shirt"]
was already 1
from the previous iteration and adding 1
to 1
is 2
. The ||
operator between two operands will cause the operand that is “truthy” (the 2
on the left side) to be assigned to totals[item.type]
(currently totals["shirt"]
. This means after this iteration of the reducer, totals
looks like { shirt: 2 , pants: 1, socks: 1 }
.
In the fifth iteration, item
is { type: "pants", price: 2343 }
, so item.type
is "pants"
. This also means the reference totals[item.type]
(total["pants"]
will be 2
since totals["pants"]
was already 1
from the previous iteration and adding 1
to 1
is 2
. The ||
operator between two operands will cause the operand that is “truthy” (the 2
on the left side) to be assigned to totals[item.type]
(currently totals["pants"]
. This means after this iteration of the reducer, totals
looks like { shirt: 2 , pants: 2, socks: 1 }
.
In the sixth iteration, item
is { type: "socks", price: 542 }
, so item.type
is "socks"
. This also means the reference totals[item.type]
(total["socks"]
will be 2
since totals["socks"]
was already 1
from the previous iteration and adding 1
to 1
is 2
. The ||
operator between two operands will cause the operand that is “truthy” (the 2
on the left side) to be assigned to totals[item.type]
(currently totals["socks"]
. This means after this iteration of the reducer, totals
looks like { shirt: 2 , pants: 2, socks: 2 }
.
In the final iteration, item
is { type: "pants", price: 123 }
, so item.type
is "pants"
. This also means the reference totals[item.type]
(total["socks"]
will be 3
since totals["pants"]
was already 2
from the previous iteration and adding 1
to 2
is 3
. The ||
operator between two operands will cause the operand that is “truthy” (the 3
on the left side) to be assigned to totals[item.type]
(currently totals["pants"]
. This means after this iteration of the reducer, totals
looks like { shirt: 2 , pants: 3, socks: 2 }
.