-
function isValidByArray(arg) { if (arg instanceof Array) { if (arg.filter(e => typeof e !== "number").length === 0) { return true; } } return false; } function setSymbol(num, sym) { if (typeof num !== "number") { throw new Error("Parameters is wrong."); } let strNum = String(num); let strFraction = ""; if (strNum.indexOf(".") !== -1) { // 소수점인 경우. const splitNum = strNum.split("."); strNum = splitNum[0]; strFraction = splitNum[1]; } // console.log(strNum, strFraction); let len = strNum.length; const cnt = Math.floor((strNum.length - 1) / 3); // 숫자가 3자리 이하인경우 if (len < 3) { return strFraction !== "" ? strNum + "." + strFraction : strNum; } let retStr = ""; for (let i = 0; i < cnt; i++) { // 뒤에서부터 3자리씩 추출. const splitStr = strNum.substr(-3); strNum = strNum.substr(0, len - (i + 1) * 3); retStr = sym + splitStr + retStr; // 만약 0 < n <= 3 사이로 마지막 loop인 경우. if (0 < strNum.length && strNum.length <= 3) { retStr = strNum + retStr; } } console.log(strFraction, retStr); if (strFraction !== "") { retStr = retStr + "." + strFraction; } return retStr; } function sum(a, b) { // a와 b를 따로 구함. let calA, calB; console.log(a, typeof a); if (typeof a === "number") { calA = a; } else if (isValidByArray(a)) { calA = a.reduce((p, c) => p + c, 0); } else if (typeof a === "function") { if (typeof a() === "number") { calA = a(); } else { throw new Error("Parameters is wrong."); } } else { throw new Error("Parameters is wrong."); } if (typeof b === "number") { calB = b; } else if (isValidByArray(b)) { calB = b.reduce((p, c) => p + c, 0); } else if (typeof b === "function") { if (typeof b() === "number") { calB = b(); } else { throw new Error("Parameters is wrong."); } } else { throw new Error("Parameters is wrong."); } console.log(calA, calB); const result = setSymbol(calA + calB, ","); return result; // return 0; }