-
-
Notifications
You must be signed in to change notification settings - Fork 207
Manchester | ITP-May-2025 | Chukwuemeke Ajuebor | Sprint 1 | Data-Groups-Arrays #771
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
Changes from all commits
0daa1b0
0baaa37
425d5bc
a801ac2
ee16e10
d7b4721
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 |
|---|---|---|
|
|
@@ -5,10 +5,32 @@ | |
| // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) | ||
| // or 'list' has mixed values (the function is expected to sort only numbers). | ||
|
|
||
| //In order to fix this we need to ensure | ||
| // (1) only an array is passed | ||
| // (2) we filter only valid numbers within the passed array and | ||
| // (3) return null if there are no valid numbers. | ||
| // filter | ||
|
|
||
| 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 nums = list.filter(x => typeof x === 'number' && !isNaN(x)); | ||
| if (nums.length === 0) return null; | ||
|
|
||
| const sorted = [...nums].sort((a, b) => a - b); | ||
| const middleIndex = Math.floor(sorted.length / 2); | ||
| let median; | ||
| if (sorted.length % 2 !== 0) { | ||
| median = sorted.slice(middleIndex, middleIndex + 1)[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. Think about when you want to use slicing to access arrays, and what you are trying to do here. Is there a way to get a value from an array that might be better suited than slicing for this line of code? |
||
| return median; | ||
| } | ||
| else { const mid = sorted.slice(middleIndex - 1, middleIndex + 1); //you are declaring and assigning at the same time | ||
| median = (mid[0] + mid [1])/2; | ||
| return median; | ||
| } | ||
| } | ||
|
|
||
| module.exports = calculateMedian; | ||
|
|
||
| /* const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,20 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| // returns a new empty array as just returning elements | ||
| if (elements.length === 0) return []; | ||
|
|
||
| // We create an empty array to repopulate | ||
| const result = []; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| const item = elements[i]; | ||
|
|
||
| // the loop takes each item and adds it | ||
| // as long as it is not in the destination array "results" | ||
| if (!result.includes(item)) { | ||
| result.push(item); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,29 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) return null; | ||
| const nums = elements.filter(x => typeof x === 'number' && !isNaN(x)); | ||
| if (nums.length === 0) return -Infinity; | ||
|
|
||
| const positives = nums.some(n => n > 0); | ||
| const negatives = nums.some(n => n < 0); | ||
|
|
||
| if (positives && negatives) { | ||
| // this helps us check and considers all values both sides of zer0 | ||
| let maxAbs = nums[0]; | ||
| for (let i = 1; i < nums.length; i++) { | ||
| if (Math.abs(nums[i]) > Math.abs(maxAbs)) { | ||
| maxAbs = nums[i]; | ||
| } | ||
| } | ||
| return maxAbs; | ||
| } else { | ||
| return Math.max(...nums); | ||
| } | ||
|
|
||
| /* if (nums.length === 1) return elements[0]; | ||
|
|
||
| const arranged = [...nums].sort((a, b) => a - b); | ||
| const highest = arranged[arranged.length - 1]; | ||
| return highest; */ | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,32 +12,41 @@ We have set things up already so that this file can see your function from the o | |
|
|
||
| const findMax = require("./max.js"); | ||
|
|
||
| //Given an array with regular integers, when passed to the max function it should return the highest | ||
| test("an array with regular integers, returns the highest number",() => {expect(findMax([30, 50, 10, 40])).toEqual(50)}); | ||
|
|
||
| // Given an empty array | ||
| // 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("single element array, returns the only item",() => {expect(findMax([23])).toEqual(23)}); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
|
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. I think you may have misunderstood what the program specification is asking for here. Try to think about what your understanding is generally - does your code behave consistently when given mixed numbers and only negative numbers? |
||
| test("an array with positive and negative numbers, returns the largest",() => {expect(findMax([-5, 15, -20, 3])).toEqual(-20)}); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("an array with just negative numbers, closest to zero",() => {expect(findMax([-9, -3, -20, -1])).toEqual(-1)}); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("an array with decimal numbers, returns the largest decimal number",() => {expect(findMax([1.1, 3.5, -4.9, 2.2])).toEqual(-4.9)}); | ||
|
|
||
| // 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("an array mixed with non-numbers, returns max ignoring the non-numbers",() => {expect(findMax(["hello", null, 42, "99", undefined, -100])).toEqual(-100)}); | ||
|
|
||
| // 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("an array with only non-numbers, returns least surprising value",() => {expect(findMax(["fan", null, undefined, {}, ""])).toEqual(-Infinity)}); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,16 @@ | ||
| function sum(elements) { | ||
| if (elements.length === 0) return 0; | ||
| let total = 0 | ||
| let num = false; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| const item = elements[i]; | ||
| if (typeof item === "number" && !isNaN(item)) { //this excludes non numbers before totalling | ||
| total += item; | ||
| num = true; | ||
| } | ||
| } | ||
| return num? total: null; //if num true i.e actual numbers exist, return total otherwise return null | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Can you fix this code? | ||
| function doubleAllNumbers(numbers) { | ||
| let doubledNumbers = []; | ||
|
|
||
| for (let n of numbers) { | ||
| doubledNumbers.push(n * 2); | ||
| } | ||
|
|
||
| return doubledNumbers; | ||
| } | ||
|
|
||
| const myNums = [10, 20, 30]; | ||
| doubleAllNumbers(myNums); | ||
| console.log(myNums); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const express = require('express'); | ||
| const app = express(); // ✅ This creates the app | ||
|
|
||
| const PORT = process.env.PORT || 3000; | ||
|
|
||
| app.get('/', (req, res) => { | ||
| res.send('Hello, world!'); | ||
| }); | ||
|
|
||
| app.listen(PORT, () => { | ||
| console.log(`🚀 Server running at http://localhost:${PORT}`); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| function calculateMean(list) { | ||
| let total = 0; | ||
|
|
||
| for (const item of list) { | ||
| total += item; | ||
| } | ||
| const mean = total/list.length | ||
| return mean; | ||
| } | ||
|
|
||
| //console.log(calculateMean([10,20,30,40,50])) | ||
|
|
||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
|
|
||
| return median; | ||
| } | ||
|
|
||
|
|
||
| const salaries = [10, 20, 30, 40, 60, 80, 80]; | ||
|
|
||
| const sal_mean = calculateMean(salaries) | ||
| const sal_med = calculateMedian(salaries) | ||
|
|
||
| console.log(sal_mean) | ||
| console.log(sal_med) | ||
| console.log(salaries) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| test("calculates the mean of a list of numbers", () => { | ||
| const list = [3, 50, 7]; | ||
| const currentOutput = calculateMean(list); | ||
| const targetOutput = 20; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3 | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| test("calculates the median of a list of odd length", () => { | ||
| const list = [10, 20, 30, 50, 60]; | ||
| const currentOutput = calculateMedian(list); | ||
| const targetOutput = 30; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); |
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.
Why are you expanding an array into an array here? Can you think of a way to simplify this?