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
29 changes: 25 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
}
if (!Array.isArray(list) || list.length === 0) {
return null;
}
const numbersOnly = list.filter(element => typeof element === 'number');
if (numbersOnly.length === 0) {
return null;
}

const sortedList = [...numbersOnly].sort((a, b) => a - b);

if (sortedList.length % 2 === 0) {
const middleIndex = Math.floor(sortedList.length / 2);
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
}
const middleIndex = Math.floor(sortedList.length / 2);

return sortedList[middleIndex];



}

const salaries = [10, 40, 50, 70, 90]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you have test files, put the tests into those ones rather than leaving them in the main file

const median = calculateMedian(salaries);

module.exports = calculateMedian;

10 changes: 9 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
function dedupe() {}
function dedupe(list) {
if (!Array.isArray(list)) {
return [];
}

return [...new Set(list)];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of a set!

}

module.exports = dedupe;
8 changes: 8 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function findMax(elements) {
if (!Array.isArray(elements) || elements.length === 0) {
return -Infinity;
}
const numbersOnly = elements.filter(item => typeof item === 'number' && !isNaN(item));
if (numbersOnly.length === 0) {
return -Infinity;
}
return Math.max(...numbersOnly);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you maybe try re-implementing this without using the built-in max function?

}

module.exports = findMax;
12 changes: 12 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function sum(elements) {
if (!Array.isArray(elements) || elements.length === 0) {
return 0;
}

const numbersOnly = elements.filter(item => typeof item === "number" && !isNaN(item));

if (numbersOnly.length === 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you have 0 elements, do you need this if condition here?

return 0;
}

return numbersOnly.reduce((acc, curr) => acc + curr, 0);

}

module.exports = sum;