Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions 5_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* const startDateStr = '02 Aug 1985';
const endDateStr = '03 Aug 1985'; */

const startDateStr = '31 Jan 2022';
const endDateStr = '03 Feb 2021';
Comment on lines +4 to +5
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?

function dateDiff(startDateStr = '1 Jan 2022', endDateStr = '1 Jan 2023', unit = 'seconds') {
const startDate = new Date(startDateStr);
const endDate = new Date(endDateStr);
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)



}

// 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;
// }
Comment on lines +20 to +33
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

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?


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




18 changes: 18 additions & 0 deletions 5_2.js
Original file line number Diff line number Diff line change
@@ -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;
Comment on lines +2 to +8
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

};

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'}
24 changes: 24 additions & 0 deletions 5_3.js
Original file line number Diff line number Diff line change
@@ -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 ({, })

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


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 += 2) {
sum = sum + i;
}

return (sum);
}

console.log(iterativeOddSumTo(1)) // 1
console.log(iterativeOddSumTo(10)) // 25