From 43f8d684cebc0ef4dc89ea086994cced369f4191 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Thu, 27 Apr 2023 22:48:50 -0400 Subject: ex2.2 use Array.reduce to do computations in components --- part2/courseinfo2/src/App.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/part2/courseinfo2/src/App.js b/part2/courseinfo2/src/App.js index f1bc1ef..a4647ec 100644 --- a/part2/courseinfo2/src/App.js +++ b/part2/courseinfo2/src/App.js @@ -32,12 +32,13 @@ const Content = (props) => { } const Total = (props) => { - const one = props.parts.parts[0].exercises - const two = props.parts.parts[1].exercises - const three = props.parts.parts[2].exercises + const parts = props.parts + const initialValue = 0 - return ( -

Number of exercises {one + two + three}

+ return (

Number of exercises {parts.reduce((sum, part) => { + return sum += part.exercises + + }, initialValue)}

) } @@ -47,7 +48,8 @@ const Course = (props) => { return ( <>
- + + ) } -- cgit v1.2.3