From 2858c8c0f86a350de59c93df52728f92d9b65e01 Mon Sep 17 00:00:00 2001 From: AranovskyAlex Date: Thu, 16 Mar 2023 21:32:17 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0?= =?UTF-8?q?=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 5_1.js | 26 ++++++++++++++++++++++++++ 5_2.js | 18 ++++++++++++++++++ 5_3.js | 25 +++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 5_1.js create mode 100644 5_2.js create mode 100644 5_3.js diff --git a/5_1.js b/5_1.js new file mode 100644 index 0000000..1e71f88 --- /dev/null +++ b/5_1.js @@ -0,0 +1,26 @@ +/* const startDateStr = '02 Aug 1985'; +const endDateStr = '03 Aug 1985'; */ + +const startDateStr = '31 Jan 2022'; +const endDateStr = '03 Feb 2021' +function dateDiff(startDateStr, endDateStr, unit = 'seconds') { + const startDate = new Date(startDateStr); + const endDate = new Date(endDateStr); + let result = Math.abs(endDate - startDate); + switch (unit) { + case 'seconds' : + result = result / 1000; + break; + case "minutes" : + result = result / 60000; + break; + case "hours" : + result = result / (1000 * 60 * 60); + break; + case "days" : + result = result / (1000 * 60 * 60 * 24); + break; + } + console.log(result); +} +dateDiff(endDateStr, startDateStr, 'seconds'); \ No newline at end of file diff --git a/5_2.js b/5_2.js new file mode 100644 index 0000000..391ad1e --- /dev/null +++ b/5_2.js @@ -0,0 +1,18 @@ +function optimizer(data) { + const result = {}; +for (const [key, value] of Object.entries(data)) { + result[key.toLowerCase()] = parseFloat(value).toFixed(2); + +} + + return result; +}; + +const priceData = { + Apples: '23.4', + BANANAS: '48', + oRAngGEs: '48.7584', +}; + +let updatedPriceData = optimizer(priceData); +console.log(updatedPriceData) // {apples: '23.40', bananas: '48.00', oranges: '48.76'} \ No newline at end of file diff --git a/5_3.js b/5_3.js new file mode 100644 index 0000000..beade5c --- /dev/null +++ b/5_3.js @@ -0,0 +1,25 @@ +function recursiveOddSumTo(number) { + if (number == 1) + return 1; + if (number % 2 == 1) + return (recursiveOddSumTo(number - 1) + number); + + return (recursiveOddSumTo(number - 1)) +}; +console.log(recursiveOddSumTo(1)) // 1 +console.log(recursiveOddSumTo(10)) // 25 + + +function iterativeOddSumTo(number) { + let sum = 0; + + for (i = 1; i <= number; i++) { + if (i % 2 == 1) + sum = sum + i; + } + + return (sum); +} + +console.log(iterativeOddSumTo(1)) // 1 +console.log(iterativeOddSumTo(10)) // 25 \ No newline at end of file From ca15830eafb62c09fc59b7792200a21e476134cc Mon Sep 17 00:00:00 2001 From: AranovskyAlex Date: Mon, 20 Mar 2023 21:49:12 +0200 Subject: [PATCH 2/2] Fixed after code review --- 5_1.js | 52 ++++++++++++++++++++++++++++++++++------------------ 5_3.js | 5 ++--- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/5_1.js b/5_1.js index 1e71f88..1f0cddc 100644 --- a/5_1.js +++ b/5_1.js @@ -2,25 +2,41 @@ const endDateStr = '03 Aug 1985'; */ const startDateStr = '31 Jan 2022'; -const endDateStr = '03 Feb 2021' -function dateDiff(startDateStr, endDateStr, unit = 'seconds') { +const endDateStr = '03 Feb 2021'; +function dateDiff(startDateStr = '1 Jan 2022', endDateStr = '1 Jan 2023', unit = 'seconds') { const startDate = new Date(startDateStr); const endDate = new Date(endDateStr); - let result = Math.abs(endDate - startDate); - switch (unit) { - case 'seconds' : - result = result / 1000; - break; - case "minutes" : - result = result / 60000; - break; - case "hours" : - result = result / (1000 * 60 * 60); - break; - case "days" : - result = result / (1000 * 60 * 60 * 24); - break; + const result = Math.abs(endDate - startDate); + const unitDivisor = { + 'seconds': result / 1000, + 'minutes': result / 60000, + 'hours': result / (1000 * 60 * 60), + 'days': result / (1000 * 60 * 60 * 24) + + + } - console.log(result); + + // switch (unit) { + // case 'seconds' : + // result = result / 1000; + // break; + // case "minutes" : + // result = result / 60000; + // break; + // case "hours" : + // result = result / (1000 * 60 * 60); + // break; + // case "days" : + // result = result / (1000 * 60 * 60 * 24); + // break; + // } + console.log(unitDivisor[unit]); } -dateDiff(endDateStr, startDateStr, 'seconds'); \ No newline at end of file + +dateDiff(endDateStr, startDateStr, 'days'); + +console.log(result); + + + diff --git a/5_3.js b/5_3.js index beade5c..a5e4f1e 100644 --- a/5_3.js +++ b/5_3.js @@ -13,9 +13,8 @@ console.log(recursiveOddSumTo(10)) // 25 function iterativeOddSumTo(number) { let sum = 0; - for (i = 1; i <= number; i++) { - if (i % 2 == 1) - sum = sum + i; + for (i = 1; i <= number; i += 2) { + sum = sum + i; } return (sum);