-
-
Notifications
You must be signed in to change notification settings - Fork 94
ITP-Jan | NW | Jovy So | Module-Data-Groups | WEEK 1 #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
function dedupe() {} | ||
function dedupe(elements) { | ||
if (elements.length === 0) return []; | ||
else if (elements.length === 1) return elements; | ||
|
||
for (let i = elements.length -1; i >= 0; i--) { | ||
for (let j = 0; j < i; j++) { | ||
if (elements[j] === elements[i]) { | ||
elements.splice(i, 1); | ||
break; | ||
} | ||
} | ||
} | ||
return elements; | ||
}; | ||
Comment on lines
+1
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also consider using built-in functions to improve performance. |
||
|
||
module.exports = dedupe; | ||
|
||
console.log(dedupe([])); | ||
console.log(dedupe(["a", "b", "c"])); | ||
console.log(dedupe(['a','a','a','b','b','c'])); | ||
console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8])); | ||
console.log(dedupe([1, 2, 1])); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also treat "an array with only non-number values" as an array of no numbers or an empty array. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
function findMax(elements) { | ||
} | ||
function findMax(elements) { | ||
let numberOnly = elements.filter(item => typeof item === "number"); | ||
// let nonNumber = elements.filter(item => typeof item !== "number"); | ||
if (elements.length === 0) return -Infinity; | ||
else if (elements.length === 1 && elements[0] > 0) return elements[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this condition, |
||
// else if (typeof elements !== "number") return NaN | ||
else if (elements.every(item => typeof item !== "number")) return NaN; | ||
else return Math.max(...numberOnly); | ||
}; | ||
|
||
|
||
module.exports = findMax; | ||
|
||
console.log(findMax([])); | ||
console.log(findMax([10])); | ||
console.log(findMax([1, -5, 9, -10])); | ||
console.log(findMax([-1, -3, -2])); | ||
console.log(findMax([0.7, 1.2, 9.8, -11.3])); | ||
console.log(findMax(["Apple", true, 10, 98])); | ||
console.log(findMax([true, "Apple", false, "Banana"])); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,28 +16,50 @@ 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", () => { | ||
expect(findMax([])).toEqual(-Infinity); | ||
}); | ||
|
||
// 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, returns that number", () => { | ||
expect(findMax([10])).toEqual(10); | ||
}); | ||
|
||
// 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 number, returns largest number", () => { | ||
expect(findMax([1, -5, 9, -10])).toEqual(9); | ||
}); | ||
|
||
// 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 number, returns the closest one to zero", () => { | ||
expect(findMax([-1, -3, -2])).toEqual(-1); | ||
}); | ||
|
||
// 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 number, returns largest decimal number", () => { | ||
expect(findMax([0.7, 1.2, 9.8, -11.3])).toEqual(9.8); | ||
}); | ||
|
||
|
||
// 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, returns largest number", () => { | ||
expect(findMax(["Apple", true, 10, 98])).toEqual(98); | ||
}); | ||
|
||
|
||
// 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, returns largest number", () => { | ||
expect(findMax([true, "Apple", false, "Banana"])).toEqual(NaN); | ||
}); | ||
Comment on lines
+63
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by "the largest number"? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,24 @@ | ||
function sum(elements) { | ||
} | ||
if (elements.length === 0) return 0; | ||
else if (elements.every(item => typeof item !== "number")) return NaN; | ||
else if (elements.length === 1) return elements[0]; | ||
|
||
let sumOfElements = 0; | ||
for (let i = 0; i < elements.length; i++) { | ||
if (typeof elements[i] === "number") { | ||
sumOfElements += elements[i]; | ||
} | ||
} | ||
return sumOfElements; | ||
}; | ||
Comment on lines
+2
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also consider filter out all non-number values first, and then just work on the numbers in the filtered array. |
||
|
||
module.exports = sum; | ||
|
||
console.log(sum([])); | ||
console.log(sum([1])); | ||
console.log(sum([-1, 1, -2, 3])); | ||
console.log(sum([1.1, 2.2, 3, 4.5])); | ||
console.log(sum(['hey', 10, 'hi', 60, 10])); | ||
console.log(sum(['hey', true, 'hi'])); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,24 +13,41 @@ 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", () => { | ||
expect(sum([])).toEqual(0); | ||
}); | ||
|
||
// Given an array with just one number | ||
// When passed to the sum function | ||
// Then it should return that number | ||
test("given just one number array, returns that number", () => { | ||
expect(sum([1])).toEqual(1); | ||
}) | ||
|
||
// 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 number, returns correct total sum", () => { | ||
expect(sum([-1, 1, -2, 3])).toEqual(1); | ||
}); | ||
|
||
// 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 number, returns correct total sum", () => { | ||
expect(sum([1.1, 2.2, 3, 4.5])).toEqual(10.8); | ||
}); | ||
Comment on lines
+37
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 So the following could happen
Can you find a more appropriate way to test a value (that involves decimal number calculations) for equality? |
||
|
||
// 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, returns sum of numerical elements and ignore non-numerical values", () => { | ||
expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80); | ||
}); | ||
|
||
// 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", () => { | ||
expect(sum(['hey', true, 'hi'])).toEqual(NaN); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
// 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) { | ||
// return true; | ||
// } | ||
// } | ||
// return false; | ||
// } | ||
|
||
function includes(list, target) { | ||
for (let index = 0; index < list.length; index++) { | ||
const element = list[index]; | ||
if (element === target) { | ||
for (const items of list) { | ||
if (items === target) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
module.exports = includes; | ||
|
||
console.log(includes(["a", "b", "c", "d"], "c")); | ||
console.log(includes([1, 2, 3, 4], "a")); | ||
console.log(includes([1, 2, 2, 3], 2)); | ||
console.log(includes([])); | ||
console.log(includes(["b", "z", null, "a"], null)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The arrays specified in
median.test.js
are already sorted by coincidence.Typically, to find the median value of an array, we first need to sort it. However, to prevent modifying
list
directly, it's best to work with a cloned version instead.Could you add one or more statements to create a cloned copy of
list
and then sort the numbers in the cloned array?