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
24 changes: 19 additions & 5 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// 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;
function calculateMedian(list){
if(!Array.isArray(list)){
return null;
}
const num = [];
for(let i = 0; i < list.length ; i++){
if (typeof list[i] === "number"){
num.push(list[i]);
}
}
if (num.length === 0){
return null;
}
num.sort((a, b) => a - b);
const middleIndex = Math.floor(num.length / 2);
if (num.length % 2 === 1){
return num[middleIndex];
}
return (num[middleIndex-1] + num[middleIndex])/2;
}

module.exports = calculateMedian;
11 changes: 10 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
function dedupe() {}
function dedupe(array) {
const result = [];
for(let i = 0; i < array.length ; i++){
if(!result.includes(array[i])){
result.push(array[i]);
}
}
return result;
}
module.exports = dedupe;
13 changes: 10 additions & 3 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ E.g. dedupe([1, 2, 1]) returns [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.todo("given an empty array, it returns an empty array");
test("given an empty array, returns 0", () => {
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 duplicate, returns original array", () => {
expect(dedupe([2,7,9])).toEqual([2,7,9]);
});
// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
test("given an array of string or numbers, returns with no duplicates", () => {
expect(dedupe([4, "banana", 4, "banana", 2])).toEqual([4, "banana", 2]);
});
14 changes: 13 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function findMax(elements) {
if (elements.length === 0){
return -Infinity;
}
let max = -Infinity;
for(let item of elements){
if(typeof item !== "number"){
continue;
}
if(item > max){
max = item;
}
}
return max;
}

module.exports = findMax;
27 changes: 21 additions & 6 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,43 @@ 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([])).toBe(-Infinity);
});

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

test("given an empty array, returns 0", () => {
expect(findMax([78])).toBe(78);
});
// 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 numbers, returns largest number", () => {
expect(findMax([-8, 10, 45, -60])).toBe(45);
});
// 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 negative numbers, returns closest to 0", () => {
expect(findMax([-8, -7, -5, -60])).toBe(-5);
});
// 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, returns largest number", () => {
expect(findMax([3.7, 5.8, 4.5, 60.8])).toBe(60.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 number", () => {
expect(findMax([100, "hello", 94])).toBe(100);
});
// 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 non-numbers, returns -Infinity", () => {
expect(findMax(["hi", "maryam", "here"])).toBe(-Infinity);
});
12 changes: 11 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function sum(elements) {
}
let result = 0;
if(elements.length === 0){
return 0;
}
for(let i = 0; i < elements.length; i++){
if(typeof(elements[i]) == "number"){
result = result + elements[i];
}
}
return result;

}
module.exports = sum;
25 changes: 20 additions & 5 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,39 @@ 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")

test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});

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

test("given an array with just 1 value, returns that value", () => {
expect(sum([50])).toBe(50);
});
// 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 numbers, returns correct sum", () => {
expect(sum([9, 2, 3, -4])).toBe(10);
});
// 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 numbers, returns correct sum", () => {
expect(sum([4.5, 9.2, 9, 3.2])).toBe(25.9);
});
// 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 only values", () => {
expect(sum([5, "banana", 2])).toBe(7);
});
// 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, returns 0", () => {
expect(sum(["hi", "here"])).toBe(0);
});
9 changes: 3 additions & 6 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +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(const element of list){
if(element === target ){
return true;
}
}
return false;
}

module.exports = includes;
module.exports = includes;
Loading