diff options
author | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-04-27 22:48:50 -0400 |
---|---|---|
committer | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-04-27 22:48:50 -0400 |
commit | 43f8d684cebc0ef4dc89ea086994cced369f4191 (patch) | |
tree | d639edf2f0ea681f19f7e6c824d151ce0bfb6687 | |
parent | a0b8dc6005af538f37a36d6069224b52ac9d060f (diff) |
ex2.2 use Array.reduce to do computations in components
-rw-r--r-- | part2/courseinfo2/src/App.js | 14 |
1 files 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 ( - <p>Number of exercises {one + two + three}</p> + return (<p> Number of exercises {parts.reduce((sum, part) => { + return sum += part.exercises + + }, initialValue)} </p> ) } @@ -47,7 +48,8 @@ const Course = (props) => { return ( <> <Header name={header} /> - <Content parts={parts} /> + <Content parts={parts} /> + <Total parts={parts} /> </> ) } |