Skip to content

Commit 1c8df67

Browse files
authored
Merge pull request #1462 from CodeYourFuture/update-solutions
Bring solutions branch up to date
2 parents 7e39c55 + a077516 commit 1c8df67

69 files changed

Lines changed: 9079 additions & 9590 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/FUNDING.yml‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,2 @@
1-
# These are supported funding model platforms
2-
31
github: CodeYourFuture
4-
patreon: # Replace with a single Patreon username
5-
open_collective: # Replace with a single Open Collective username
6-
ko_fi: # Replace with a single Ko-fi username
7-
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8-
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9-
liberapay: # Replace with a single Liberapay username
10-
issuehunt: # Replace with a single IssueHunt username
11-
otechie: # Replace with a single Otechie username
12-
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
132
custom: https://codeyourfuture.io/donate

‎.github/ISSUE_TEMPLATE/config.yml‎

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

‎.github/ISSUE_TEMPLATE/pd-assignment.yml‎

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

‎.github/ISSUE_TEMPLATE/tech-ed-assignment.yml‎

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

‎.github/pull_request_template.md‎

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Validate PR Metadata
2+
on:
3+
pull_request_target:
4+
types:
5+
- labeled
6+
- unlabeled
7+
- opened
8+
- edited
9+
- reopened
10+
11+
jobs:
12+
validate_pr_metadata:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: CodeYourFuture/actions/validate-pr-metadata@main
17+
with:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎.gitignore‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules
22
.DS_Store
33
.vscode
4-
**/.DS_Store
4+
**/.DS_Store
5+
.idea
6+
package-lock.json

‎.vscode/extensions.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"streetsidesoftware.code-spell-checker",
66
"eamodio.gitlens",
77
"ritwickdey.LiveServer",
8-
"vsliveshare.vsliveshare"
8+
"vsliveshare.vsliveshare",
9+
"Orta.vscode-jest"
910
]
1011
}

‎HOW_TO_REVIEW.md‎

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

‎Sprint-1/fix/median.js‎

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
11
// Fix this implementation
22
// Start by running the tests for this function
3-
// If you're in the week-1 directory, you can run npm test -- fix to run the tests in the fix directory
3+
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory
44

5-
// Old Code:
6-
7-
// function calculateMedian(list) {
8-
// const middleIndex = Math.floor(list.length / 2);
9-
// const median = list.splice(middleIndex, 1)[0];
10-
// return median;
11-
// }
5+
// Hint: Please consider scenarios when 'list' isn't an array, is empty,
6+
// or contains values that aren't numbers (the function is expected to throw - see the tests).
127

138
// Explanation:
14-
// The original implementation has several mistakes:
15-
// 1 - .splice() mutates or changes the original array. We don't want this to happen.
16-
// 2 - We actually don't need .splice() or any other method to find a median.
17-
// 3 - We must sort the array before we search for the median.
18-
// 4 - We should check if the 'list' parameter actually has numbers, otherwise try to filter
19-
// out all non-numeric values and throw an error or return null if there are no numeric values.
20-
21-
22-
// Example of a function calculating median and returning null in case no numeric values provided.
9+
// Check the input first and throw if there is no sensible median to return.
10+
// Sort a copy so the caller's array is not mutated, then read the middle
11+
// element, or the average of the two middle elements when the length is even.
2312

2413
function calculateMedian(list) {
2514
if (!Array.isArray(list)) {
26-
return null;
15+
throw new Error("calculateMedian requires an array of numbers");
2716
}
28-
const onlyNumberList = list.filter(item => typeof item === 'number' && !isNaN(item));
29-
30-
if (!onlyNumberList.length) {
31-
return null;
32-
} else {
33-
const sortedList = onlyNumberList.sort((current, next) => current - next);
34-
const indexNearMiddleOfArray = Math.floor(sortedList.length / 2);
35-
if (sortedList.length % 2 === 0) {
36-
return (sortedList[indexNearMiddleOfArray - 1] + sortedList[indexNearMiddleOfArray]) / 2;
37-
} else {
38-
return sortedList[indexNearMiddleOfArray];
17+
for (const item of list) {
18+
if (typeof item !== "number") {
19+
throw new Error("calculateMedian requires an array of numbers");
3920
}
4021
}
22+
if (list.length === 0) {
23+
throw new Error("calculateMedian requires a non-empty array");
24+
}
25+
26+
const sorted = [...list].sort((a, b) => a - b);
27+
const middleIndex = Math.floor(sorted.length / 2);
28+
29+
if (sorted.length % 2 === 0) {
30+
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
31+
}
32+
return sorted[middleIndex];
4133
}
4234

4335
module.exports = calculateMedian;

0 commit comments

Comments
 (0)