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
38 changes: 35 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,41 @@
// 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;
let newArray = [];
if(!Array.isArray(list))
{
return null;
}

for (const item of list)
{
if (typeof item === 'number')
{
newArray.push(item);
}

}
if (newArray.length === 0)
{
return null;
}
newArray = newArray.sort((a, b) => a-b);
if(newArray.length %2 === 0)
{
const rightIndex = newArray.length/2
const leftIndex = rightIndex - 1;
const median = (newArray[rightIndex] + newArray[leftIndex])/2;
return median;

}
else{

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

module.exports = calculateMedian;
18 changes: 17 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
function dedupe() {}
function dedupe(list) {
if (list.length=== 0)
{
return [];
}
let result = [];
for (let item of list)
{
if(!result.includes(item))
{
result.push(item)
}
}
return result;

}
module.exports = dedupe;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ 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("given an empty array, it returns an empty array", ()=>{
const list = [];
expect(dedupe(list)).toEqual([]);
});
test
("array with no duplicate should return copy of original", ()=>{
let list = [1,2,4,6,9,8];
expect(dedupe(list)).toEqual([1,2,4,6,9,8]);
});
test("shoiuld remove the duplicate value and preserving the first occurebce",()=>{
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5,1,2,3,8]);
})
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
Expand Down
15 changes: 15 additions & 0 deletions 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;
}
let max = -Infinity ;
for (const item of elements)
{
if (typeof item === 'number')
{
max = Math.max(max , item);
}


}
return max;
}

module.exports = findMax;
29 changes: 28 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,34 @@ 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 should return -Infinity", () => {
const list = [];
expect(findMax(list)).toEqual(-Infinity);
});
test("Array with one number should return that number", () => {
const list =[7];
expect(findMax(list)).toEqual(7);
});
test("An array with both positive and negative number should return the largest overall", () =>{
const list = [-10, 5, 30, -3];
expect(findMax(list)).toEqual(30);
});
test("Array with just negative number should return the closest on to zero", () =>{
const list = [-10, -3, -20,-7,-4]
expect(findMax(list)).toEqual(-3);
});
test("An array with decimal number should return largest decimal number", ()=>{
const list = [1.2, 3.5, 2.8]
expect(findMax(list)).toEqual(3.5);
});
test("An array with non-numeric values should return the max and ignore non-numeric", () =>{
const list = ['hey', 10, 'hi', 60, 50];
expect(findMax(list)).toEqual(60);
});
test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> {
const list = ['a', 'b', 'hello'];
expect(findMax(list)).toEqual(-Infinity);
})

// Given an array with one number
// When passed to the max function
Expand Down
15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
if (elements.length === 0)
{
return 0;
}
let total = 0 ;
for (const item of elements)
{
if (typeof item === 'number')
{
total = total + item;
}


}
return total;
}

module.exports = sum;
29 changes: 28 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,34 @@ 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 should return -Infinity", () => {
const list = [];
expect(sum(list)).toEqual(0);
});
test("Array with one number should return that number", () => {
const list =[7];
expect(sum(list)).toEqual(7);
});
test("An array with both positive and negative number should return the largest overall", () =>{
const list = [-10, 5, 30, -3];
expect(sum(list)).toEqual(22);
});
test("Array with just negative number should return the closest on to zero", () =>{
const list = [-10, -3, -20,-7,-4]
expect(sum(list)).toEqual(-44);
});
test("An array with decimal number should return largest decimal number", ()=>{
const list = [1.2, 3.5, 2.8]
expect(sum(list)).toEqual(7.5);
});
test("An array with non-numeric values should return the max and ignore non-numeric", () =>{
const list = ['hey', 10, 'hi', 60, 50];
expect(sum(list)).toEqual(120);
});
test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> {
const list = ['a', 'b', 'hello'];
expect(sum(list)).toEqual(0);
})

// Given an array with just one number
// When passed to the sum function
Expand Down
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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];
for (const element of list) {
if (element === target) {
return true;
}
Expand Down
Loading