Skip to content

Commit 22d78b2

Browse files
author
Enice-Codes
committed
Complete all Sprint 1 tasks: dedupe, sum, max, median, includes
1 parent de6889c commit 22d78b2

9 files changed

Lines changed: 105 additions & 20 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

88
function calculateMedian(list) {
9-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
9+
if (!Array.isArray(list)){
10+
return null;
11+
}
12+
const numbersOnly= list.filter(item => typeof item === "number");
13+
if(numbersOnly.length === 0){
14+
return null ;
15+
}
16+
numbersOnly.sort((a,b)=> a-b);
17+
const middleIndex = Math.floor(numbersOnly.length / 2); // finding the middle index of numbers only
18+
if (numbersOnly.length %2 === 0){
19+
return (numbersOnly[middleIndex-1] + numbersOnly[middleIndex])/2 ;
20+
// finding mdeian for even numbers
21+
22+
} else {
23+
return numbersOnly[middleIndex];
24+
}
25+
const median = numbersOnly.splice(middleIndex, 1)[0]; //
1126
return median;
1227
}
1328

‎Sprint-1/implement/dedupe.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
return arr.filter((item ,index ) => arr.indexOf(item)=== index);
3+
4+
5+
6+
}
7+
8+
module.exports = dedupe;

‎Sprint-1/implement/dedupe.test.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,28 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
1616
// Given an empty array
1717
// When passed to the dedupe function
1818
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
19+
test("given an empty array, it returns an empty array" , () =>{
20+
expect(dedupe([])).toEqual([]);
21+
22+
});
2023

2124
// Given an array with no duplicates
2225
// When passed to the dedupe function
2326
// Then it should return a copy of the original array
27+
test("returns a copy of the array when there are no duplicates", () => {
28+
const original = [1, 2, 3];
29+
const result = dedupe(original);
30+
31+
expect(result).toEqual([1, 2, 3]); // same values
32+
expect(result).not.toBe(original); // but a different array in memory
33+
});
2434

2535
// Given an array of strings or numbers
2636
// When passed to the dedupe function
2737
// Then it should return a new array with duplicates removed while preserving the
2838
// first occurrence of each element from the original array.
39+
40+
test("return duplicate array of strings or numbers", () => {
41+
expect(dedupe([5,1,1,2,3,2,5,8])).toEqual([5,1,2,3,8]);
42+
43+
});

‎Sprint-1/implement/max.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
function findMax(elements) {
1+
function findMax(arr) {
2+
const numbers = arr.filter((x) => typeof x === "number" && !Number.isNaN(x));
3+
4+
if (numbers.length === 0) return -Infinity;
5+
6+
let max = numbers[0];
7+
for (let i = 1; i < numbers.length; i++) {
8+
if (numbers[i] > max) {
9+
max = numbers[i];
10+
}
11+
}
12+
return max;
213
}
314

4-
module.exports = findMax;
15+
module.exports = findMax;

‎Sprint-1/implement/max.test.js‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,51 @@ const findMax = require("./max.js");
1515
// Given an empty array
1616
// When passed to the max function
1717
// Then it should return -Infinity
18-
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
18+
// Delete this test.todo and replace it with a test
19+
test("given an empty array, returns -Infinity", () => {
20+
expect(findMax([])).toEqual(-Infinity);
21+
});
2022

2123
// Given an array with one number
2224
// When passed to the max function
2325
// Then it should return that number
24-
26+
test("given one number, when passed by max function, return that number", () => {
27+
expect(findMax([1])).toEqual(1);
28+
});
2529
// Given an array with both positive and negative numbers
2630
// When passed to the max function
2731
// Then it should return the largest number overall
32+
test("array with positive and negative numbers returns largest number", () => {
33+
expect(findMax([-2, 4, -3, 5, -6, 7])).toEqual(7);
34+
});
2835

2936
// Given an array with just negative numbers
3037
// When passed to the max function
3138
// Then it should return the closest one to zero
39+
test("array with negative numbers , returns one closest to zero",() =>{
40+
expect(findMax([ -2,-3,-5])).toEqual(-2)
41+
});
3242

3343
// Given an array with decimal numbers
3444
// When passed to the max function
3545
// Then it should return the largest decimal number
46+
test("array with decimal number,return largest decimal number",()=>{
47+
expect(findMax([1.2,2.2,3.1,5.5])).toEqual(5.5)
48+
});
3649

3750
// Given an array with non-number values
3851
// When passed to the max function
3952
// Then it should return the max and ignore non-numeric values
40-
53+
test("array with non-number values ,return max number and ignore non-numeric values",()=>{
54+
const input = ["hi", 6, 2, 1, 7, "we", "put"];
55+
const result = findMax(input);
56+
expect(result).toEqual(7);
57+
});
4158
// Given an array with only non-number values
4259
// When passed to the max function
4360
// Then it should return the least surprising value given how it behaves for all other inputs
61+
test("array with only non-number values, return the least surprising value", () => {
62+
const input = ["no", "hat", "dot", "cape"];
63+
const result = findMax(input);
64+
expect(result).toEqual(-Infinity);
65+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
function sum(elements) {
1+
function sum(arr) {
2+
return arr
3+
.filter(item => typeof item === 'number' && !isNaN(item))
4+
.reduce((total, num) => total + num, 0);
25
}
36

47
module.exports = sum;

‎Sprint-1/implement/sum.test.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,46 @@ In this kata, you will need to implement a function that sums the numerical elem
55
E.g. sum([10, 20, 30]), target output: 60
66
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
77
*/
8-
9-
const sum = require("./sum.js");
8+
const sum = require('./sum');
109

1110
// Acceptance Criteria:
1211

1312
// Given an empty array
1413
// When passed to the sum function
1514
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
15+
test("given an empty array, returns 0",() => {
16+
expect(sum([])).toBe(0);
17+
});
1718

1819
// Given an array with just one number
1920
// When passed to the sum function
2021
// Then it should return that number
22+
test("given an array with one number,return that number",() => {
23+
expect(sum(2)).toBe(2);
24+
});
2125

2226
// Given an array containing negative numbers
2327
// When passed to the sum function
2428
// Then it should still return the correct total sum
29+
test("given negative numbers, sums correctly", () => {
30+
expect(sum([-5, 10, -3])).toBe(2);
31+
});
2532

2633
// Given an array with decimal/float numbers
2734
// When passed to the sum function
2835
// Then it should return the correct total sum
29-
36+
test("given floats, sums correctly", () => {
37+
expect(sum([1.5, 2.5, -1])).toBe(3);
38+
});
3039
// Given an array containing non-number values
3140
// When passed to the sum function
3241
// Then it should ignore the non-numerical values and return the sum of the numerical elements
33-
42+
test("given mixed types, ignores non-numeric values", () => {
43+
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
44+
});
3445
// Given an array with only non-number values
3546
// When passed to the sum function
3647
// Then it should return the least surprising value given how it behaves for all other inputs
48+
test("given an array with non-number values, return the least surprising value", () =>{
49+
expect(sum(["up","to","top"])).toBe(0);
50+
});

‎Sprint-1/refactor/includes.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Refactor the implementation of includes to use a for...of loop
2-
32
function includes(list, target) {
4-
for (let index = 0; index < list.length; index++) {
5-
const element = list[index];
3+
for (const element of list) {
64
if (element === target) {
75
return true;
86
}

‎Sprint-1/refactor/includes.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Refactored version of includes should still pass the tests below:
22

3-
const includes = require("./includes.js");
3+
const includes = require("./includes");
44

55
test("returns true when target is in array", () => {
66
const currentOutput = includes(["a", "b", "c", "d"], "c");

0 commit comments

Comments
 (0)