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
19 changes: 16 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
// 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;
if (!Array.isArray(list)){ //check if variable is array
return null;
}
const numbersOnly = list.filter(item => typeof item === "number" && !isNaN(item));
if (numbersOnly.length == 0){
return null;
}
const sorted =[...numbersOnly].sort((a,b) => a-b ); //we created a new arrey where we saved sorted element (as string) and turned them to numbers.

const middleIndex = Math.floor(sorted.length / 2);
if (sorted.length % 2 !== 0){ //for odds length of arrey
return sorted[middleIndex]
}
else{
return (sorted[middleIndex -1] + sorted[middleIndex]) / 2; //for even length of arrey
}
}

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) {
if (array.length == 0){ // check if array is empty
return [];
}
let mySet = new Set(array); // create set mySet
let dedupeArray = Array.from (mySet); // create new array from the set mySet
return dedupeArray;
}

module.exports = dedupe;
21 changes: 19 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@ E.g. dedupe([1, 2, 1]) target output: [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("given an empty array, it returns an empty array", () => {
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("given an array with no duplicates, it should return a copy of the original array", () => {
const currentOutput = dedupe([5, 1, 2, 3, 8]);
const targetOutput = [5, 1, 2, 3, 8];
expect(currentOutput).toEqual(targetOutput);
});
// 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
test("given an array with strings or number, it should remove the duplicate values, preserving the first occurence of each element", () => {
const currentOutput = dedupe([5, "1", "1", 5, 8]);
const targetOutput = [5, "1", 8];

console.log(currentOutput);
console.log(targetOutput);

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) {
//filter array for numbersOnly
const numbersOnly = elements.filter(item => !isNaN(item));
if (numbersOnly.length == 0){ // check if array is empty
return -Infinity;
}
if (numbersOnly.length === 1){ //check if they has one element
return numbersOnly[0];
}
let max = numbersOnly[0];// find a max number
for (let i=1; i<numbersOnly.length; i++){
if (numbersOnly[i]> max){
max = numbersOnly[i];
}
}
return max;
}

module.exports = findMax;
37 changes: 36 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,63 @@ 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, it should return -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("Given an array with one number, it should return that number", () => {
const currentOutput = findMax([1]);
const targetOutput = 1;
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("Given an array with both positive and negative numbers it should return the largest number overall", () => {
const currentOutput = findMax([-2, 2, 3, -3]);
const targetOutput = 3;
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("Given an array with just negative numbers, it should returnthe closest one to zero", () => {
const currentOutput = findMax([-1, -2, -3, -4]);
const targetOutput = -1;
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("given an array with decimal numbers, it should return the largest decimal number", () => {
const currentOutput = findMax([0.2, 0.45, 5.3, 1.5]);
const targetOutput = 5.3;
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("given an array with non-number values, it should return the max and ignore non-numeric values", () => {
const currentOutput = findMax(["a", "!", 3, 5, "0"]);
const targetOutput = 5;
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("given an array with only non-number values, it should return the least surprising value given how it behaves for all other inputs", () => {
const currentOutput = findMax(["2", "3", "$", "%"]);
const targetOutput = "3";
expect(currentOutput).toEqual(targetOutput);
});
17 changes: 17 additions & 0 deletions 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){ // check if array is empty
return 0;
}
if (elements.length === 1){ //check if array has one element
return elements[0];
}
let sum = 0;
let isNumbersPresent = elements.some(item => typeof item === 'number' && !isNaN(item));
for (let i =0; i<elements.length; i++){ //sum of elements
if (typeof elements[i] === 'number'){ //check if element is number
sum += elements[i];
}
if (isNumbersPresent == false && !isNaN(elements[i])){ //1) check if array doesnt have numbers; 2)check if element can be conversted to number
sum += parseFloat(elements[i]); // add converted to num element
}
}
return sum
}

module.exports = sum;
34 changes: 32 additions & 2 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,61 @@ E.g. sum([10, 20, 30]), target output: 60
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
*/

const sum = require("./sum.js");
const sum = require("./sum.js");

// Acceptance Criteria:

// 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, it should return 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 array with just one number, it should return that number", () => {
const currentOutput = sum ([1]);
const targetOutput = 1;
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 array containing negative numbers, it should still return the correct total sum", () => {
const currentOutput = sum ([-2, -3, -4, -5]);
const targetOutput = -14;
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/float numbers, it should return the correct total sum", () => {
const currentOutput = sum ([2.5, 3.1, 4.9]);
const targetOutput = 10.5;
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-number values, it should return the sum of the numerical elements", () => {
const currentOutput = sum (["a", "!", 3, 5, ","]);
const targetOutput = 8;
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-number values, it should return the least surprising value given how it behaves for all other inputs", () => {
const currentOutput = sum (["a", "!", "3", "5", ","]);
const targetOutput = 8;
expect(currentOutput).toEqual(targetOutput);
});
5 changes: 2 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// 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;
}
}
Expand Down
Loading