Skip to content

LONDON | MAY2025 | EMILIANO URUENA | DATA_GROUPS | Sprint-1 #546

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 3 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
30 changes: 25 additions & 5 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
// 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;
// First of all, we check if list is an array.
if (!Array.isArray(list)) {
return null
}
// the next code from 13 to 16create a new Array called listFiltered checking if the elements are numbers and no NaN>
const listFiltered = list.filter(checkElement);

Choose a reason for hiding this comment

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

Using an anonymous function here will make this method more concise (instead of checkElement).

function checkElement(n){
return typeof n === "number" && n != isNaN;
}
// return null if there are not numbers in the list.
if (listFiltered.length == 0) {
return null
}
// sort the list
listFiltered.sort();
const middleIndex = Math.floor(listFiltered.length / 2);
// check for even or odd amount of numbers in the list
if (listFiltered.length % 2 == 0) {
const median = (listFiltered[middleIndex - 1] + listFiltered[middleIndex]) / 2;
return median
} else {
const median = listFiltered.splice(middleIndex, 1)[0];
return median

Choose a reason for hiding this comment

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

Using an Inline return statement on this line and line 28 is best practice

}
}

module.exports = calculateMedian;
module.exports = {calculateMedian}
2 changes: 1 addition & 1 deletion Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// passing all the tests...
// Fix the implementation of calculateMedian so it passes all tests

const calculateMedian = require("./median.js");
const {calculateMedian} = require("./median.js");

describe("calculateMedian", () => {
[
Expand Down
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}

function dedupe(list) {
if (list.length === 0 ) return list;
let newList = []
for (i = 0 ; i < list.length; i ++){
if (!newList.includes(list[i])) {
newList.push(list[i]);
}
}
return newList;
}
module.exports = {dedupe}
38 changes: 33 additions & 5 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const dedupe = require("./dedupe.js");
const {dedupe} = require("./dedupe.js");
/*
Dedupe Array

Expand All @@ -8,20 +8,48 @@ In this kata, you will need to deduplicate the elements of an array

E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) target output: [1, 2]
*/
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");
//test.todo("given an empty array, it returns an empty array");

// 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 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

describe("dedupe", () => {
[
{ input: ['a','a','a','b','b','c'], expected:['a','b','c']},
{ input: [5, 1, 1, 2, 3, 2, 5, 8], expected:[5, 1, 2, 3, 8]},
{ input: [1, 2, 1], expected:[1, 2]},
{ input: [], expected:[]},
].forEach(({input, expected}) =>
it(`return dedupe list for [${input}]`,()=> expect(dedupe(input)).toEqual(expected))
);
[
{ input: [], expected:[]},
].forEach(({input, expected}) =>
it(`return empty array for [${input}]`,()=> expect(dedupe(input)).toEqual(expected))
);
[
{ input: [1,4,6,9], expected:[1,4,6,9]},
].forEach(({input, expected}) =>
it(`return copy of the original with no duplicates in array> [${input}]`,()=> expect(dedupe(input)).toEqual(expected))
);
[
{ input: [1,'a',0,'g',1,9,'e',2,1,'k',1,3,'a'], expected:[1,'a',0,'g',9,'e',2,'k',3]},
].forEach(({input, expected}) =>
it(`String and numbers in an array: [${input}]`,()=> expect(dedupe(input)).toEqual(expected))
);
}
);

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) {
}
if (elements.length === 0) return '-Infinity';
if (elements.length === 1) return elements;
let maxNum = []

Choose a reason for hiding this comment

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

Why is maxNum an array? A number type as the return value is most appropriate for this function.

for (i = 0 ; i < elements.length ; i++){
if (typeof elements[i] === 'number'){
if (maxNum.length === 0){
maxNum[0] = elements[i]; //give a value for the first time
} else {
if (elements[i] > maxNum[0]){
maxNum[0] = elements[i];
}
}
}
}
if (maxNum.length === 0){ return 'Family' } else { return maxNum }

}
module.exports = findMax;
51 changes: 50 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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.todo("given an empty array, returns -Infinity");

// Given an array with one number
// When passed to the max function
Expand All @@ -41,3 +41,52 @@ test.todo("given an empty array, returns -Infinity");
// 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

describe("findMax", () => {
[
{ input: [30, 50, 10, 40], expected: [50]},
{ input: ['hey', 10, 'hi', 60, 10], expected: [60]},
{ input: [79], expected: [79]},


].forEach(({input, expected}) =>
it(`return max number for [${input}]`,() => expect(findMax(input)).toEqual(expected))

);
[
{ input: [], expected: '-Infinity'},

].forEach(({input, expected}) =>
it(`return -Infinity to empty array: [${input}]`,() => expect(findMax(input)).toEqual(expected))

);
[
{ input: [-3,5,8,34,-45,0], expected: [34]},

].forEach(({input, expected}) =>
it(`an array with both positive and negative numbers should return the largest number overall [${input}]`,() => expect(findMax(input)).toEqual(expected))

);
[
{ input: [-34,-13,-65,-678,-45366], expected: [-13]},

].forEach(({input, expected}) =>
it(`An array with just negative numbers should return the closest one to zero: [${input}]`,() => expect(findMax(input)).toEqual(expected))

);
[
{ input: [0.143,0.31426,0.81], expected: [0.81]},

].forEach(({input, expected}) =>
it(`An array with decimal number should return the largest decimal number [${input}]`,() => expect(findMax(input)).toEqual(expected))

);
[
{ input: ['e','r','n','a','l','g','l'], expected: 'Family'},

Choose a reason for hiding this comment

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

Is 'Family' the best value to return in this case? Why not return something like null.


].forEach(({input, expected}) =>
it(`An array with only non-number values should return the least surprising value given how it behaves for all other inputs [${input}]`,() => expect(findMax(input)).toEqual(expected))

);
}
);
19 changes: 18 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
function sum(elements) {
}

if (elements.length === 0) return 0;
// if (elements.length === 1) return elements;
let sum = 0
let noNum = []
for (i = 0 ; i < elements.length ; i++){
if (typeof elements[i] === 'number'){
sum += elements[i];
} else {
noNum.push(elements[i])

Choose a reason for hiding this comment

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

Is there a better way to check if the array contains no numbers, instead of making a new array and adding all the elements to it? Every array in your code will use more memory, and for very large inputs this will be inefficient.

}
}
if (elements.length === noNum.length) return 'Family'

Choose a reason for hiding this comment

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

Again, is returning 'Family' the most appropriate here? Why not either 0 or null?

return sum;
// if (maxNum.length === 0){ return 'Family' } else { return maxNum }


}
//console.log(sum([2,3,5,3]))
module.exports = sum;
17 changes: 16 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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.todo("given an empty array, returns 0")

// Given an array with just one number
// When passed to the sum function
Expand All @@ -34,3 +34,18 @@ test.todo("given an empty array, returns 0")
// 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

describe("sum", () => {
[
{input: [10, 20, 30], expected: 60},
{input: ['hey', 10, 'hi', 60, 10], expected: 80 },
{input: [], expected: 0},
{input: [9], expected: 9},
{input: [-4,-2,-53,-3], expected: -62},
{input: [0.2,1.5,0.4], expected: 2.1 },
{input: ['a','g'], expected: 'Family'},
].forEach(({input, expected}) =>
it(`It should return the expected value with the input [${input}]`,() => expect(sum(input)).toEqual(expected))
)
}
);
7 changes: 3 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// 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) {
for ( let index of list){
if (index === target) {
return true;
}
}
}
return false;
}
Expand Down