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
20 changes: 17 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
// 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)) return null;

const numbers = [];
for (const x of list) {
if (typeof x === "number" && !isNaN(x)) {
numbers.push(Number(x));
}
}
Comment on lines +11 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code work.

This is also a good opportunity to practice using the array's .filter() method to simplify the code on lines 11-16.

if (numbers.length === 0) return null;
numbers.sort((a, b) => a - b);
const length = numbers.length;
const middleIndex = Math.floor(length / 2);
if (length % 2 === 0) {
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
} else {
return numbers[middleIndex];
}
}

module.exports = calculateMedian;
16 changes: 15 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(arr) {

if(arr.length===0) return arr;
const dedupeArray=[]
for(let i=0;i<arr.length;i++){

if(!dedupeArray.includes(arr[i])){
dedupeArray.push(arr[i])
}
}
return dedupeArray;


}
Comment on lines +1 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is not consistently formatted.

Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?

module.exports=dedupe;
45 changes: 37 additions & 8 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const dedupe = require("./dedupe.js");
const dedupe = require("./dedupe");
/*
Dedupe Array

Expand All @@ -16,12 +16,41 @@ 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");
describe("dedupe()", () => {
[{ input: [], expected: [] }].forEach(({ input, expected }) =>
it(`given an empty array, it returns an empty array [${input}]`, () => {
expect(dedupe(input)).toEqual(expected);
})
);
// Given an array with no duplicates
// Then it should return a copy of the original array
[
{ input: [1, 2, 3, 4], expected: [1, 2, 3, 4] },
{
input: ["apples", "banana", "orange"],
expected: ["apples", "banana", "orange"],
},
{ input: [-1, 7, 1], expected: [-1, 7, 1] },
].forEach(({ input, expected }) =>
it(`should return same input values [${input}] without duplicate`, () => {
expect(dedupe(input)).toEqual(expected);
}));
Comment on lines +25 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should fail if the function returns the original array (instead of a copy of the original array).

The current test checks only if both the original array and the returned array contain identical elements.
In order to validate the returned array is a different array, we need an additional check.

Can you find out what this additional check is?


// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
// When passed to the dedupe function
// 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

// 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
[
{ input: [1, 2, 5, 5, "a", 5, 10, 10, "a"], expected: [1, 2, 5, "a", 10] },
{
input: ["apple", "banana", "orange", "apple", "banana", 1, 3, 4, 1],
expected: ["apple", "banana", "orange", 1, 3,4],
},
].forEach(({ input, expected }) =>
it(`should return deduplicated array for [${input}]`, () => {
expect(dedupe(input)).toEqual(expected);
})
);

});
17 changes: 17 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
function findMax(elements) {
if (!Array.isArray(elements)) return "invalid elements";
if (elements.length === 0) return Infinity;
const number = [];
for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number" && !Number.isNaN(elements[i])) {
number.push(elements[i]);
}
}
if (number.length === 0) return "invalid elements";
Comment on lines +3 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could consider treating both empty array and arrays that contains only non-numeric values as
"arrays that do not contain any number".

let max = number[0];

for (let i = 1; i < number.length; i++) {
if (max < number[i]) {
max = number[i];
}
}
return max;
}

module.exports = findMax;
83 changes: 64 additions & 19 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,73 @@ 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");
describe("findMax()", () => {
[{ input: [], expected: Infinity }].forEach(({ input, expected }) =>
it(`should return ${expected} for empty [${input}]`, () => {
expect(findMax(input)).toEqual(expected);
})
);

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

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
[{ input: [50], expected: 50 }].forEach(({ input, expected }) =>
it(`should return ${expected} for array [${input}]`, () => {
expect(findMax(input)).toEqual(expected);
})
);

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

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
[{ input: [2, 5, 6, -1, 0, 25, -30], expected: 25 }].forEach(
({ input, expected }) => {
it(`should return ${expected} for positive and negative numbers in the array`, () => {
expect(findMax(input)).toEqual(expected);
});
}
);

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
[{ input: [-1, -10, -7, -20], expected: -1 }].forEach(({ input, expected }) =>
it(`should return negative number nearest to zero`, () => {
expect(findMax(input)).toEqual(expected);
})
);

// 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
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

[{ input: [0.5, 0.1, 0.56, 0.8], expected: 0.8 }].forEach(
({ input, expected }) =>
it(`should return the largest decimal number from the array`, () => {
expect(findMax(input)).toEqual(expected);
})
);

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
[
{ input: ["edak", "ofonime", "", "@", -4, 10, 6, 50, -100], expected: 50 },
].forEach(({ input, expected }) =>
it(`should return max numerical value from the array`, () => {
expect(findMax(input)).toEqual(expected);
})
);

// 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
[{ input: ["peter", "", "@", "Hi"], expected: "invalid elements" }].forEach(
({ input, expected }) =>
it(`should return "invalid elements" for non-numeric values`, () => {
expect(findMax(input)).toEqual(expected);
})
);
});
Comment on lines +68 to +88
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a string representing a valid numeric literal (for example, "300") is compared to a number,
JavaScript first converts the string into its numeric equivalent before performing the comparison.
As a result, the expression 20 < "300" evaluates to true.

To test if the function can correctly ignore non-numeric values,
consider including a string such as "300" in the relevant test cases.

15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
if (!Array.isArray(elements)) return "invalid elements";
if(elements.length===0) return 0;
const number = [];
for (const x of elements) {
if (typeof x === "number" && !Number.isNaN(x)) {
number.push(x);
}
}
if(number.length===0) return "invalid elements";
let sumOfNum=0;
for(let i=0; i<number.length; i++){

sumOfNum+=number[i]
}
return sumOfNum
}

module.exports = sum;
88 changes: 64 additions & 24 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,67 @@ 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")

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

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

// 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

// 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
describe("sum()", () => {
// Given an empty array
// When passed to the sum function
// Then it should return 0
[{ input: [], expected: 0 }].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);

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

[{ input: [30], expected: 30 }].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

[{ input: [-1, -3, -4, -11], expected: -19 }].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

[{ input: [0.5, 0.2, 0.11, 0.89, 0.3], expected: 2 }].forEach(
({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
Comment on lines +47 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.

So the following could happen

  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 );                // This fail
  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 );   // This pass
  expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 );   // This fail

  console.log(1.2 + 0.6 + 0.005 == 1.805);  // false
  console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // false

Can you find a more appropriate way to test a value (that involves decimal number calculations) for equality?

Suggestion: Look up

  • Checking equality in floating point arithmetic in JavaScript
  • Checking equality in floating point arithmetic with Jest


// 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

[
{ input: ["evan", 3, "mike", 20, 6, "", "/", , , 20], expected: 49 },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to explicitly specify undefined instead of leaving the element blank.

].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);

// 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
[
{ input: ["evan", "mike", "", "/", , ,], expected: "invalid elements" },
].forEach(({ input, expected }) =>
it(`should return ${expected} for [${input}]`, () => {
expect(sum(input)).toEqual(expected);
})
);
});
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 (const element of list) {
if (element === target) {
return true;
}
Expand Down
Loading