Skip to content

ITP-Jan | NW | Jovy So | Module-Data-Groups | WEEK 1 #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
16 changes: 13 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory

function calculateMedian(list) {
// const oddMiddleIndex = Math.floor(list.length / 2);
// const oddMedian = list.splice(oddMiddleIndex, 1)[0];
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
}
// const evenMiddleIndex = list.length / 2;
// const evenMedian = ((list.splice(evenMiddleIndex -1 ,1)[0]) + (list.splice(evenMiddleIndex ,1)[0])) / 2;
if (list.length % 2 === 1) return middleIndex ;
else return (list[middleIndex - 1] + list[middleIndex]) / 2;
};

Comment on lines +6 to +14
Copy link

Choose a reason for hiding this comment

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

The arrays specified in median.test.js are already sorted by coincidence.

Typically, to find the median value of an array, we first need to sort it. However, to prevent modifying list directly, it's best to work with a cloned version instead.

Could you add one or more statements to create a cloned copy of list and then sort the numbers in the cloned array?


module.exports = calculateMedian;

console.log(calculateMedian([1, 2, 3, 4, 5]));
console.log(calculateMedian([1, 2, 3, 4, 5, 6]));
console.log(calculateMedian([1, 2, 3, 4]));
console.log(calculateMedian([1, 2, 3]));
23 changes: 22 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
function dedupe() {}
function dedupe(elements) {
if (elements.length === 0) return [];
else if (elements.length === 1) return elements;

for (let i = elements.length -1; i >= 0; i--) {
for (let j = 0; j < i; j++) {
if (elements[j] === elements[i]) {
elements.splice(i, 1);
break;
}
}
}
return elements;
};
Comment on lines +1 to +14
Copy link

Choose a reason for hiding this comment

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

You can also consider using built-in functions to improve performance.
Some of the built-in functions/methods you can consider are Array.prototype.indexOf() and Array.prototype.includes().


module.exports = dedupe;

console.log(dedupe([]));
console.log(dedupe(["a", "b", "c"]));
console.log(dedupe(['a','a','a','b','b','c']));
console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8]));
console.log(dedupe([1, 2, 1]));
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");
test("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("given a no duplicated array, return original array", () => {
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);
});

// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
test("given a duplicated array", () => {
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
20 changes: 18 additions & 2 deletions Sprint-1/implement/max.js
Copy link

Choose a reason for hiding this comment

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

You can also treat "an array with only non-number values" as an array of no numbers or an empty array.

Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function findMax(elements) {
}
function findMax(elements) {
let numberOnly = elements.filter(item => typeof item === "number");
// let nonNumber = elements.filter(item => typeof item !== "number");
if (elements.length === 0) return -Infinity;
else if (elements.length === 1 && elements[0] > 0) return elements[0];
Copy link

Choose a reason for hiding this comment

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

With this condition, findMax(["10"]) would return "10", findMax([true]) would return true.

// else if (typeof elements !== "number") return NaN
else if (elements.every(item => typeof item !== "number")) return NaN;
else return Math.max(...numberOnly);
};


module.exports = findMax;

console.log(findMax([]));
console.log(findMax([10]));
console.log(findMax([1, -5, 9, -10]));
console.log(findMax([-1, -3, -2]));
console.log(findMax([0.7, 1.2, 9.8, -11.3]));
console.log(findMax(["Apple", true, 10, 98]));
console.log(findMax([true, "Apple", false, "Banana"]));
24 changes: 23 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,50 @@ const findMax = require("./max.js");
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("given an array with one number, returns that number", () => {
expect(findMax([10])).toEqual(10);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("given an array with both positive and negative number, returns largest number", () => {
expect(findMax([1, -5, 9, -10])).toEqual(9);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("given an array with just negative number, returns the closest one to zero", () => {
expect(findMax([-1, -3, -2])).toEqual(-1);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("given an array with decimal number, returns largest decimal number", () => {
expect(findMax([0.7, 1.2, 9.8, -11.3])).toEqual(9.8);
});


// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
test("given an array with non-number values, returns largest number", () => {
expect(findMax(["Apple", true, 10, 98])).toEqual(98);
});


// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
test("given an array with only non-number values, returns largest number", () => {
expect(findMax([true, "Apple", false, "Banana"])).toEqual(NaN);
});
Comment on lines +63 to +65
Copy link

Choose a reason for hiding this comment

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

What do you mean by "the largest number"? NaN is "Not a number".

22 changes: 21 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function sum(elements) {
}
if (elements.length === 0) return 0;
else if (elements.every(item => typeof item !== "number")) return NaN;
else if (elements.length === 1) return elements[0];

let sumOfElements = 0;
for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number") {
sumOfElements += elements[i];
}
}
return sumOfElements;
};
Comment on lines +2 to +13
Copy link

Choose a reason for hiding this comment

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

You can also consider filter out all non-number values first, and then just work on the numbers in the filtered array.


module.exports = sum;

console.log(sum([]));
console.log(sum([1]));
console.log(sum([-1, 1, -2, 3]));
console.log(sum([1.1, 2.2, 3, 4.5]));
console.log(sum(['hey', 10, 'hi', 60, 10]));
console.log(sum(['hey', true, 'hi']));


19 changes: 18 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,41 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
test("given an empty array, returns 0", () => {
expect(sum([])).toEqual(0);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given just one number array, returns that number", () => {
expect(sum([1])).toEqual(1);
})

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given an array containing negative number, returns correct total sum", () => {
expect(sum([-1, 1, -2, 3])).toEqual(1);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("given an array with decimal/float number, returns correct total sum", () => {
expect(sum([1.1, 2.2, 3, 4.5])).toEqual(10.8);
});
Comment on lines +37 to +39
Copy link

Choose a reason for hiding this comment

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

Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.

So the following could happen

  expect(sum([1.2, 0.6, 0.005])).toEqual(1.805);                // This fail
  expect(sum([1.2, 0.6, 0.005])).toEqual(1.8049999999999997);   // This pass
  expect(sum([0.005, 0.6, 1.2])).toEqual(1.8049999999999997);   // This fail

  console.log(1.2 + 0.6 + 0.005 == 1.805);  // false
  console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // false

Can you find a more appropriate way to test a value (that involves decimal number calculations) for equality?


// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values and return the sum of the numerical elements
test("given an array containing non-number values, returns sum of numerical elements and ignore non-numerical values", () => {
expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80);
});

// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
test("given an array with only non-number values", () => {
expect(sum(['hey', true, 'hi'])).toEqual(NaN);
});
21 changes: 18 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
// Refactor the implementation of includes to use a for...of loop

// function includes(list, target) {
// for (let index = 0; index < list.length; index++) {
// const element = list[index];
// if (element === target) {
// return true;
// }
// }
// return false;
// }

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element === target) {
for (const items of list) {
if (items === target) {
return true;
}
}
return false;
}

module.exports = includes;

console.log(includes(["a", "b", "c", "d"], "c"));
console.log(includes([1, 2, 3, 4], "a"));
console.log(includes([1, 2, 2, 3], 2));
console.log(includes([]));
console.log(includes(["b", "z", null, "a"], null));