Skip to content

ITP JAN 25 | KatarzynaKazimierczuk | Data Groups | Sprint1 #474

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
9 changes: 7 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

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

if (list.length % 2 === 0) {
const median = (list[middleIndex-1] + list[middleIndex]) /2;
return median;
Copy link

Choose a reason for hiding this comment

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

You could return the result of (list[middleIndex-1] + list[middleIndex]) /2 right away instead of assign the value to a median variable as median won't be used again.

} else {
return list[middleIndex];
}
}

module.exports = calculateMedian;
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
function dedupe() {}
function dedupe(arr) {
Copy link

Choose a reason for hiding this comment

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

Good job using includes() and push() with new array. There is another solution using Set object As a bonus challenge, can you come up with another solution as well?

const modified = [];

arr.forEach(el => {
if (!modified.includes(el)) {
modified.push(el) ;
}
})
return modified;
}
module.exports = dedupe;
34 changes: 24 additions & 10 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]

// Acceptance Criteria:

// 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");
describe("dedupe", () => {
test("given an empty array, it returns an empty array", () => {
// Given an empty array
// When passed to the dedupe function
// Then it should return 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
// 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 returns a copy of the original array", () => {
const array = [1, 2, 3];
const result = dedupe(array);
expect(result).toEqual([1, 2, 3]);
});

// 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
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurrence of each element
test("given an array with strings or numbers, it removes duplicate values and keeps one of each element", () => {
const array = [1, 1, 2, 3];
const result = dedupe(array);
expect(result).toEqual([1, 2, 3]);
});
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function findMax(elements) {
}
// inf includes non-numerical and numberical
//ignore non numerical
let filtered = elements.filter(element => typeof element === 'number')

//check if empty
if (filtered.length === 0) {
return -Infinity;
} else if (filtered.length === 1) {
return filtered[0]
} else {
//find max
return Math.max(...filtered)
Copy link

Choose a reason for hiding this comment

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

Can you explain why we need to use spread operator with Math.max() here?

}

}


module.exports = findMax;
53 changes: 42 additions & 11 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,63 @@ We have set things up already so that this file can see your function from the o

const findMax = require("./max.js");

// Given an empty array
// 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");
describe('find max', () => {
// Given an empty array
// When passed to the max function
// Then it should return -Infinity
test('given an empty array, it returns -Infinity', () => {
expect(findMax([])).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number

// Given an array with both positive and negative numbers
test('given an array with one number, it returns that number', () => {
expect(findMax([1])).toEqual(1)
});
// // 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 positive and negative integers, it returns the largest number overall', () => {
expect(findMax([-1, 1])).toEqual(1)
})
// 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 of negative integers, it returns the closes one to 0', () => {
expect(findMax([-2,-1])).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, it returns the largest decimal number', () => {
expect(findMax([0.97, 0.48, 0.2])).toEqual(0.97)
})


// 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, it returns the max and ignore non-numbers', () => {
expect(findMax(['cat', 1, 5])).toEqual(5)
})

// Given an array with only non-number values


// Given an array with 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
// Then it should return the max and ignore non-numeric values
test('given an array with non-number values, it returns the max and ignore non-numbers', () => {
expect(findMax(['cat', 1, 5])).toEqual(5)
})
});