Skip to content

Домашка 5#4

Open
AranovskyAlex wants to merge 2 commits intosharkich:lesson-5from
AranovskyAlex:Lesson_5_Aranovsky
Open

Домашка 5#4
AranovskyAlex wants to merge 2 commits intosharkich:lesson-5from
AranovskyAlex:Lesson_5_Aranovsky

Conversation

@AranovskyAlex
Copy link
Copy Markdown

No description provided.

Comment thread 5_1.js Outdated

const startDateStr = '31 Jan 2022';
const endDateStr = '03 Feb 2021'
function dateDiff(startDateStr, endDateStr, unit = 'seconds') {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no default values for startDateStr and startDateStr

Comment thread 5_1.js Outdated
Comment on lines +10 to +23
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;
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid copy/paste let's create an object with keys - unit and values - numbers.

Comment thread 5_1.js Outdated
function dateDiff(startDateStr, endDateStr, unit = 'seconds') {
const startDate = new Date(startDateStr);
const endDate = new Date(endDateStr);
let result = Math.abs(endDate - startDate);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's avoid using let

Comment thread 5_3.js Outdated
let sum = 0;

for (i = 1; i <= number; i++) {
if (i % 2 == 1)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid checking every odd number?

Comment thread 5_1.js

dateDiff(endDateStr, startDateStr, 'days');

console.log(result);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2023-03-22 at 11 59 30 AM

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix an error

Comment thread 5_1.js
Comment on lines +20 to +33
// 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;
// }
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment

Comment thread 5_1.js
Comment on lines +4 to +5
const startDateStr = '31 Jan 2022';
const endDateStr = '03 Feb 2021';
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need these variables?

Comment thread 5_1.js
// break;
// }
console.log(unitDivisor[unit]);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are you returning in this function?

Comment thread 5_3.js
@@ -0,0 +1,24 @@
function recursiveOddSumTo(number) {
if (number == 1)
return 1;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the if with brackets ({, })

Comment thread 5_3.js
if (number == 1)
return 1;
if (number % 2 == 1)
return (recursiveOddSumTo(number - 1) + number);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the main logic as a default return of the function

Comment thread 5_2.js
Comment on lines +2 to +8
const result = {};
for (const [key, value] of Object.entries(data)) {
result[key.toLowerCase()] = parseFloat(value).toFixed(2);

}

return result;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the Array.reduce method here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants