Skip to content

Commit 88a4388

Browse files
author
JorvanW
committed
removed previous code in order to pass criteria
1 parent aded465 commit 88a4388

19 files changed

Lines changed: 47 additions & 223 deletions

‎Sprint-1/fix/median.js‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,10 @@
55
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

8-
function calculateMedian(list) {
9-
list = list.filter(element => typeof element === 'number');
10-
list.sort((a, b) => a - b);
11-
12-
if (list.length % 2 === 0){
13-
const middleIndexR = Math.floor(list.length / 2);
14-
const middleIndexL = middleIndexR - 1
15-
const evenMedian = (list[middleIndexL] + list[middleIndexR]) / 2;
16-
17-
return evenMedian
18-
} else {
19-
const middleIndex = Math.floor(list.length / 2);
20-
const median = list[middleIndex];
21-
return median;
22-
}
23-
8+
function calculateMedian(list) {
9+
const middleIndex = Math.floor(list.length / 2);
10+
const median = list.splice(middleIndex, 1)[0];
11+
return median;
2412
}
2513

26-
27-
28-
module.exports = calculateMedian;
14+
module.exports = calculateMedian;

‎Sprint-1/fix/median.test.js‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ describe("calculateMedian", () => {
2727
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
2828
);
2929

30-
// it("doesn't modify the input array [3, 1, 2]", () => {
31-
// const list = [3, 1, 2];
32-
// calculateMedian(list);
33-
// expect(list).toEqual([3, 1, 2]);
34-
// });
30+
it("doesn't modify the input array [3, 1, 2]", () => {
31+
const list = [3, 1, 2];
32+
calculateMedian(list);
33+
expect(list).toEqual([3, 1, 2]);
34+
});
3535

36-
// [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
37-
// it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
38-
// );
36+
[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
37+
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
38+
);
3939

40-
// [
41-
// { input: [1, 2, "3", null, undefined, 4], expected: 2 },
42-
// { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
43-
// { input: [1, "2", 3, "4", 5], expected: 3 },
44-
// { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 },
45-
// { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
46-
// { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
47-
// ].forEach(({ input, expected }) =>
48-
// it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
49-
// );
50-
});
40+
[
41+
{ input: [1, 2, "3", null, undefined, 4], expected: 2 },
42+
{ input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
43+
{ input: [1, "2", 3, "4", 5], expected: 3 },
44+
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 },
45+
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
46+
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
47+
].forEach(({ input, expected }) =>
48+
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
49+
);
50+
});

‎Sprint-1/implement/dedupe.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
function dedupe(list) {
2-
return [...new Set(list)];
3-
}
1+
function dedupe() {}

‎Sprint-1/implement/max.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
function findMax(elements) {
2-
elements = elements.filter(element => typeof element === 'number');
3-
if (elements.length === 0){
4-
return -Infinity;
5-
}
6-
return Math.max(...elements);
72
}
3+
84
module.exports = findMax;

‎Sprint-1/implement/sum.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
function sum(elements) {
2-
elements = elements.filter(element => typeof element === 'number');
3-
let total = 0;
4-
5-
for (let element of elements) {
6-
total += element;
7-
// += means to add to the current value and assign as result
8-
// elements are individual items inside a collection (not just string)
9-
}
10-
11-
return total;
122
}
133

144
module.exports = sum;

‎Sprint-1/refactor/includes.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Refactor the implementation of includes to use a for...of loop
22

33
function includes(list, target) {
4-
for (const element of list) {
4+
for (let index = 0; index < list.length; index++) {
5+
const element = list[index];
56
if (element === target) {
67
return true;
78
}
89
}
910
return false;
1011
}
1112

12-
1313
module.exports = includes;

‎Sprint-2/debug/address.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// Predict and explain first...
2-
/* To specify house number the console.log should use. address.houseNumber.
3-
without it, It would show as undefined */
42

53
// This code should log out the houseNumber from the address object
64
// but it isn't working...
@@ -14,4 +12,4 @@ const address = {
1412
postcode: "XYZ 123",
1513
};
1614

17-
console.log(`My house number is ${address.houseNumber}`);
15+
console.log(`My house number is ${address[0]}`);

‎Sprint-2/debug/author.js‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
// This program attempts to log out all the property values in the object.
44
// But it isn't working. Explain why first and then fix the problem
55

6-
/* The code wants to log the property value and is using a for...of loop
7-
to make sure it logs everything, however Objects aren't in order and Javascript
8-
doesn't know what you want. Author is an Object and objects are not
9-
iterable which is the error. To fix this change (const value of author) to
10-
(const value of object.values(author)) to specify we want the values (not properties)
11-
in the Object which is 'author'. Using a loop allows up to add information in author
12-
without needing to make changes anywhere else while getting an updated log */
13-
146
const author = {
157
firstName: "Zadie",
168
lastName: "Smith",
@@ -19,6 +11,6 @@ const author = {
1911
alive: true,
2012
};
2113

22-
for (const value of Object.values(author)) {
14+
for (const value of author) {
2315
console.log(value);
2416
}

‎Sprint-2/debug/recipe.js‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎Sprint-2/implement/contains.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
function contains(object, property) {
2-
if (Array.isArray(object)) {
3-
throw new Error("Invalid parameter");
4-
}
5-
6-
return property in object;
7-
}
1+
function contains() {}
82

93
module.exports = contains;

0 commit comments

Comments
 (0)