{"ScriptPreparationCode":"var arr = [];\r\nfor (var i = 2000; i--;) {\r\n var str = \u0027c\u0027;\r\n if (i === 1300) {\r\n str = \u0027i\u0027;\r\n }\r\n arr.push({\r\n status: str,\r\n score: 80,\r\n score_type: 0,\r\n status_date: \u002709-05-2016\u0027\r\n });\r\n}\r\n\r\nvar _op = RegExp(/[po]/g);\r\nvar _i = RegExp(/[^i]/g);\r\nvar _c = RegExp(/[^c]/g);\r\nvar _f = RegExp(/[^f]/g);\r\nvar _xn = RegExp(/cn|nc|fn|nf/g);\r\n\r\nfunction computeStatus(arr) {\r\n arr = arr.map(obj =\u003E obj.status); /* flatten array into scores only*/\r\n arr = arr.join(\u0027\u0027) /* stringify */\r\n .replace(_op, \u0027c\u0027) /* treat passed \u0026 optional as completes */\r\n .replace(_xn, \u0027i\u0027) /* treat not started \u0026 (fail or passed) as incomplete */\r\n var Cstatus = arr.replace(_i, \u0027\u0027) || arr.replace(_f, \u0027\u0027) || arr.replace(_c, \u0027\u0027) || \u0027n\u0027;\r\n return Cstatus.charAt(0);\r\n\r\n}\r\n\r\nfunction isComplete(str) {\r\n var comps = {\r\n \u0027p\u0027: true,\r\n \u0027c\u0027: true,\r\n \u0027o\u0027: true\r\n };\r\n return comps[str] || false; //test the string if it is a completion grade status - returns boolean.\r\n}\r\n\r\nfunction computeStatus2(arr) {\r\n var stat, foundN, foundF, foundP, foundC;\r\n\r\n for (var i = arr.length; i--;) {\r\n //\u0027 Now loop through the recordset and calculate the status that the overall course should have\r\n var obj = arr[i];\r\n var keys = Object.keys(obj);\r\n stat = obj[keys[0]];\r\n\r\n if (!stat) { //consider null status as not started\r\n stat = \u0027n\u0027;\r\n foundN = true;\r\n } else {\r\n if (stat !== \u0022i\u0022) { //process all non i status - n,f,p,c,o\r\n foundC = isComplete(stat); //look for completions\r\n foundF = (stat === \u0027f\u0027); //look for failures\r\n foundN = (stat === \u0027n\u0027); //look for not started\r\n } else { //exit with status of incomplete as soon as one is found\r\n return stat;\r\n }\r\n }\r\n }\r\n\r\n if (foundN) { //\u0027if we found not started lessons and nothing else then course status is \u0022not started\u0022\r\n stat = \u0022n\u0022;\r\n if (foundC || foundF) {\r\n stat = \u0022i\u0022; //\u0027if a mix of not started lessons and completed or failed lessons status is incomplete\r\n }\r\n } else {\r\n if (foundF) {\r\n //\u0027if no lessons not started and a single lesson is failed then the course status is \u0022failed\u0022.\r\n stat = \u0022f\u0022;\r\n } else {\r\n if (foundC) {\r\n //\u0027if we get here then no lessons have been found to have been incomplete, not started or failed therefore set the lesson to \u0022complete\u0022\r\n stat = \u0022c\u0022;\r\n }\r\n }\r\n }\r\n return stat;\r\n}","TestCases":[{"Name":"REGEX STRING","Code":"computeStatus(arr)","IsDeferred":false},{"Name":"LOOP w keys lookup","Code":"computeStatus2(arr)","IsDeferred":false}]}