From df51caa3010492901dfeba057c5698ca71367cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A7=84=ED=95=98=EB=A6=BC?= Date: Wed, 30 Jun 2021 23:33:38 +0900 Subject: [PATCH] solution_1281,1342,1720 --- JinHaLim/Easy/solution_1281.js | 13 +++++++++++++ JinHaLim/Easy/solution_1342.js | 18 ++++++++++++++++++ JinHaLim/Easy/solution_1720.js | 14 ++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 JinHaLim/Easy/solution_1281.js create mode 100644 JinHaLim/Easy/solution_1342.js create mode 100644 JinHaLim/Easy/solution_1720.js diff --git a/JinHaLim/Easy/solution_1281.js b/JinHaLim/Easy/solution_1281.js new file mode 100644 index 0000000..3a5ec8c --- /dev/null +++ b/JinHaLim/Easy/solution_1281.js @@ -0,0 +1,13 @@ +/** + * @param {number} n + * @return {number} + */ +var subtractProductAndSum = function(n) { + const digit = n.toString().split('').reduce((acc,curr) => { + acc[0] *= curr; + acc[1] += +curr; + return acc; + },[1,0]); + return digit[0] - digit[1]; +}; +console.log(subtractProductAndSum(234)) \ No newline at end of file diff --git a/JinHaLim/Easy/solution_1342.js b/JinHaLim/Easy/solution_1342.js new file mode 100644 index 0000000..c863f12 --- /dev/null +++ b/JinHaLim/Easy/solution_1342.js @@ -0,0 +1,18 @@ +/** + * @param {number} num + * @return {number} + */ +var numberOfSteps = function(num) { + let count = 0; + while (num !== 0) { + count += 1; + if (num % 2 === 0) { + num = num / 2; + } + else{ + num -= 1; + } + } + return count; +}; +console.log(numberOfSteps(14)) \ No newline at end of file diff --git a/JinHaLim/Easy/solution_1720.js b/JinHaLim/Easy/solution_1720.js new file mode 100644 index 0000000..924abd7 --- /dev/null +++ b/JinHaLim/Easy/solution_1720.js @@ -0,0 +1,14 @@ +/** + * @param {number[]} encoded + * @param {number} first + * @return {number[]} + */ +var decode = function(encoded, first) { + let arr = new Array(encoded.length + 1); + arr[0] = first; + encoded.forEach((v,i) => { + arr[i+1] = v ^ arr[i]; + }); + return arr; +}; +console.log(decode([1,2,3],1)) \ No newline at end of file