{"ScriptPreparationCode":"var input = [\r\n { \u0022label\u0022: \u0022A\u0022, \u0022value\u0022: 50, \u0022children\u0022: [\r\n { \u0022label\u0022: \u0022C\u0022, \u0022value\u0022: 25, \u0022children\u0022: [\r\n { \u0022label\u0022: \u0022C1\u0022, \u0022value\u0022: 12.5, \u0022children\u0022: [\r\n { \u0022label\u0022: \u0022C1a\u0022, \u0022value\u0022: 6.25},\r\n { \u0022label\u0022: \u0022C1b\u0022, \u0022value\u0022: 6.25}\r\n ]},\r\n { \u0022label\u0022: \u0022C2\u0022, \u0022value\u0022: 12.5}\r\n ]}\r\n ]},\r\n { \u0022label\u0022: \u0022B\u0022, \u0022value\u0022: 50},\r\n { \u0022label\u0022: \u0022D\u0022, \u0022value\u0022: 25}\r\n]","TestCases":[{"Name":"getTotal.01.js","Code":"/* 01. Current implementation. Working but too complex */\r\n\r\ntotal = (function getTotal(data) {\r\n return data\r\n .map(item =\u003E item.children ? getTotal(item.children) : item.value)\r\n .reduce((a, b) =\u003E a \u002B b, 0)\r\n})(input)\r\n","IsDeferred":false},{"Name":"getTotal.02.js","Code":"/* 02. Split out the implementation and call */\r\n\r\nlet total = 0\r\n\r\nconst getTotal = data =\u003E data\r\n .map(item =\u003E item.children ? getTotal(item.children) : item.value)\r\n .reduce((a, b) =\u003E a \u002B b, 0);\r\n\r\ntotal = getTotal(input)\r\n","IsDeferred":false},{"Name":"getTotal.03.js","Code":"/* 03. Write ternary operator out as if/else statement */\r\n\r\nlet total = 0\r\n\r\nconst getTotal = data =\u003E data\r\n .map(item =\u003E {\r\n if (item.children) {\r\n return getTotal(item.children)\r\n } else {\r\n return item.value\r\n }\r\n })\r\n .reduce((a, b) =\u003E a \u002B b, 0);\r\n\r\ntotal = getTotal(input)\r\n","IsDeferred":false},{"Name":"getTotal.04.js","Code":"/* 04. Replace reduce with forEach */\r\n\r\nlet total = 0\r\n\r\nconst getTotal = data =\u003E data\r\n .map(item =\u003E {\r\n if (item.children) {\r\n return getTotal(item.children)\r\n } else {\r\n return item.value\r\n }\r\n })\r\n .forEach(item =\u003E total \u002B= item || 0)\r\n\r\ntotal = getTotal(input)\r\n","IsDeferred":false},{"Name":"getTotal.05.js","Code":"/* 05. Move map logic into forEach */\r\n\r\nlet total = 0\r\n\r\nconst getTotal = data =\u003E data.forEach(item =\u003E {\r\n if (item.children) {\r\n getTotal(item.children)\r\n } else {\r\n total \u002B= item.value\r\n }\r\n})\r\n\r\ntotal = getTotal(input)\r\n","IsDeferred":false},{"Name":"getTotal.06.js","Code":"/* 06. Move variable into function scope */\r\n\r\nconst getTotal = data =\u003E {\r\n let total = 0\r\n\r\n data.forEach(item =\u003E {\r\n if (item.children) {\r\n total \u002B= getTotal(item.children)\r\n } else {\r\n total \u002B= item.value\r\n }\r\n })\r\n\r\n return total\r\n}\r\n\r\nconst total = getTotal(input)","IsDeferred":false},{"Name":"getTotal.07.js","Code":"/* 07. A better idea... */\r\n\r\nconst getTotal = data =\u003E {\r\n let total = 0;\r\n\r\n data.forEach(item =\u003E {\r\n if (item.children) {\r\n total \u002B= getTotal(item.children)\r\n } else if (item.value) {\r\n total \u002B= item.value\r\n }\r\n\r\n })\r\n\r\n return total;\r\n}\r\n\r\nconst total = getTotal(input)\r\n","IsDeferred":false},{"Name":"getTotal.08.js","Code":"/* 08. Flatmap */\r\n\r\nconst getTotal = data =\u003E {\r\n let total = 0;\r\n\r\n data.flatMap(item =\u003E {\r\n if (item.value) {\r\n total \u002B= item.value\r\n }\r\n\r\n if (item.children) {\r\n total \u002B= getTotal(item.children)\r\n }\r\n })\r\n\r\n return total;\r\n}\r\n\r\nconst total = getTotal(input)\r\n","IsDeferred":false}]}