Skip to content

WEST MIDLANDS-JAN-ITP|SEGUN FOLAYAN|DATA GROUPS|SPRINT-1 #473

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 8 commits 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
13 changes: 8 additions & 5 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Fix this implementation
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory

module.exports = calculateMedian;

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;

if (list.length % 2 === 1) {
return list[middleIndex]; // Return the middle element for odd-length lists
}

return (list[middleIndex] + list[middleIndex - 1]) / 2; // Return the average of the two middle elements for even-length lists
}

module.exports = calculateMedian;
1 change: 1 addition & 0 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ describe("calculateMedian", () => {
expect(list).toEqual([1, 2, 3]);
});
});

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(arr) {
return [...new Set(arr)];
Copy link

Choose a reason for hiding this comment

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

Could you explain the characteristics of Set object and why we need to use the spread operator here?

Copy link
Author

Choose a reason for hiding this comment

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

  1. Uniqueness-It does not contain duplicates.
  2. No index access- IT cannot be accessed using indices like arrays.
  3. Insertion order-The values in the set are iterated in the order in which they are added.
  4. Type flexibility- A Set can store values of any type (primitive or object), and even objects can be stored as unique values (by reference).
  5. The spread operator is used to convert the Set back into an array.
  6. Thank you

}

module.exports=dedupe;

21 changes: 20 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,30 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// 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.todo("given an array with no duplicates, it should return a copy of the original array");
test.todo("given an array with strings or numbers it should remove the duplicate values, preserving the first occurrence of each element");




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 an array with no duplicates, it should return a copy of the original array", () => {
expect(dedupe([5, 1, 2, 3, 8])).toEqual([5, 1, 2, 3, 8]);
});

// 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
// Then it should remove the duplicate values, preserving the first occurrence of each element
test("given an array with strings or numbers it should remove the duplicate values, preserving the first occurrence of each element", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});
10 changes: 10 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function findMax(elements) {
const newelement = elements.filter((x)=> typeof x=== "number");
if (newelement.length === 0){
return -Infinity;
}
else {
return Math.max(...newelement)
}

;

}

module.exports = findMax;
23 changes: 23 additions & 0 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,50 @@ const findMax = require("./max.js");
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");
test("an empty array should return 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,return the number", () => {
expect(findMax([2])).toEqual(2);
});

// 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 numbers", () => {
expect(findMax([2,5,0,-1,-1])).toEqual(5);
});

// 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 numbers", () => {
expect(findMax([-1,-1,-10,-18])).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 numbers", () => {
expect(findMax([0.345,0.231, 0.999])).toEqual(0.999);
}
);

// 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("when an array with non-number values is passed, it should return the max and ignore non-numeric values", () =>{
expect(findMax([0,-1,-1,3,4,"a","b","2"])).toEqual(4);
});

// 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("when an array with only non-number values are passed, it should return -Infinity", () =>{
expect(findMax([,undefined,"one", "a", "b"])).toEqual(-Infinity);
});
Loading