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
33 changes: 30 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,36 @@
// 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;
//checking if the provided list is an Array
if (!Array.isArray(list)) {
return null;
}

//need to filter the array to check only for numbers
let filteringArr = list.filter((item) => {
return typeof item === "number";
});

//handel no numbers
if (filteringArr.length === 0) {
return null;
}
// sorting the array
filteringArr.sort((a, b) => a - b);

// calculating middle index
const middleIndex = Math.floor(filteringArr.length / 2);

// finding Odd length
if (filteringArr.length % 2 === 1) {
const median = filteringArr[middleIndex];

return median;
} else {
return (filteringArr[middleIndex - 1] + filteringArr[middleIndex]) / 2;
}

// return median;
}

module.exports = calculateMedian;
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(arr) {
let newArray = [];

for (let i = 0; i < arr.length; i++) {
if (!newArray.includes(arr[i])) {
newArray.push(arr[i]);
}
}
return newArray
}

module.exports = dedupe;
36 changes: 34 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dedupe = require("./dedupe.js");

/*
Dedupe Array

Expand All @@ -16,13 +17,44 @@ 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("if receive an empty array should return an empty ", () => {
const currentOutput = dedupe([]);
const targetOutput = [];

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("the functions should remove the duplicated characters and return it inside a new array ", () => {
const currentOutput = dedupe([1, 2]);
const targetOutput = [1, 2];

expect(currentOutput).toEqual(targetOutput);
});

// 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
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
test("the functions should remove the duplicated characters and return it inside a new array ", () => {
const currentOutput = dedupe(['a','a','a','b','b','c']);
const targetOutput = ['a','b','c'];

expect(currentOutput).toEqual(targetOutput);
});

test("the functions should remove the duplicated characters and return it inside a new array ", () => {
const currentOutput = dedupe([5, 1, 1, 2, 3, 2, 5, 8]);
const targetOutput = [5, 1, 2, 3, 8];

expect(currentOutput).toEqual(targetOutput);
});

test("the functions should remove the duplicated characters and return it inside a new array ", () => {
const currentOutput = dedupe([1, 2, 1]);
const targetOutput = [1, 2];

expect(currentOutput).toEqual(targetOutput);
});
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) {
let largestNum = -Infinity

let filteringArr = elements.filter((item) => {
return typeof item === "number";
});


for (let i = 0; i < filteringArr.length; i++) {
if (filteringArr[i] > largestNum) {

largestNum = filteringArr[i];
}
}
return largestNum;
}

module.exports = findMax;

48 changes: 47 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,74 @@ 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", () => {
const currentOutput = (findMax([]));
const targetOutput = -Infinity;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("should return the largest number", () => {
const currentOutput = (findMax([30]));
const targetOutput = 30;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test("should return the largest number", () => {
const currentOutput = (findMax([30, 50, -10, 40]));
const targetOutput = 50;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("should also return the largest negative number", () => {
const currentOutput = (findMax([-30, -50, -10, -40]));
const targetOutput = -10;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("should return the largest number even if there are decimal numbers", () => {
const currentOutput = (findMax([30.4, 30.9, 10, -40]));
const targetOutput = 30.9;

expect(currentOutput).toEqual(targetOutput);
});

// 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("should skip the string and return the largest number", () => {
const currentOutput = (findMax([30, "aa", 10, 40]));
const targetOutput = 40;

expect(currentOutput).toEqual(targetOutput);
});

// 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("should skip all the string and return should be -Infinity", () => {
const currentOutput = (findMax(["bb", "aa", "cc"]));
const targetOutput = -Infinity;

expect(currentOutput).toEqual(targetOutput);
});
11 changes: 10 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function sum(elements) {
}
let sum = 0;

let filteringArr = elements.filter((item) => {
return typeof item === "number";
});

for (let i = 0; i < filteringArr.length; i++) {
sum += filteringArr[i];
}
return sum;
}
module.exports = sum;
36 changes: 35 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,58 @@ 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, returns 0", () => {
const currentOutput = sum([]);
const targetOutput = 0;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given an only one number the test should return that number", () => {
const currentOutput = sum([20]);
const targetOutput = 20;

expect(currentOutput).toEqual(targetOutput);
});
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given an only one number the test should return that number", () => {
const currentOutput = sum([-20, -30, -2, 4]);
const targetOutput = -48;

expect(currentOutput).toEqual(targetOutput);
});

// 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 the test should return the total", () => {
const currentOutput = sum([2.0, 3.3, 1.2, 4.4]);
const targetOutput = 10.9;

expect(currentOutput).toEqual(targetOutput);
});

// 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-numbers the test should skip the string and return the total", () => {
const currentOutput = sum([3, 3.3, "aa", "4.4"]);
const targetOutput = 6.3;

expect(currentOutput).toEqual(targetOutput);
});

// 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-numbers the test should return the least surprising value given how it behaves for all other inputs", () => {
const currentOutput = sum(["cc", "3", "aa", "4.4"]);
const targetOutput = 0;

expect(currentOutput).toEqual(targetOutput);
});
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 (let element of list) {
if (element === target) {
return true;
}
Expand Down
Loading