From a395843db64498c420d14e92c090f866184d84fc Mon Sep 17 00:00:00 2001 From: Arthur <> Date: Sun, 5 Jul 2026 23:39:38 +0100 Subject: [PATCH 1/4] sprint 1 finsihed --- iteration.js | 14 ++++++++++++++ mean.js | 0 mean.test.js | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 iteration.js create mode 100644 mean.js create mode 100644 mean.test.js diff --git a/iteration.js b/iteration.js new file mode 100644 index 000000000..f8654622c --- /dev/null +++ b/iteration.js @@ -0,0 +1,14 @@ +function sumValue(list){ + + let total = 0; + + + for(const num of list){ + total += num; +} + + return total; +} + +console.log(sumValue([1,2,3,4,5,6,7,8])); + diff --git a/mean.js b/mean.js new file mode 100644 index 000000000..e69de29bb diff --git a/mean.test.js b/mean.test.js new file mode 100644 index 000000000..dc5456112 --- /dev/null +++ b/mean.test.js @@ -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); +}); From c316b7f55f4bca0b398156abd3a5df77e9b05e16 Mon Sep 17 00:00:00 2001 From: Arthur <> Date: Sun, 5 Jul 2026 23:40:29 +0100 Subject: [PATCH 2/4] try so hard to finish --- Doubleallnumber.js | 14 +++++++ Fail fast and succeed | 16 ++++++++ Sprint-1/fix/median.js | 28 ++++++++++++- Sprint-1/implement/dedupe.js | 15 ++++++- Sprint-1/implement/dedupe.test.js | 27 ++++++++++++- Sprint-1/implement/max.js | 21 ++++++++++ Sprint-1/implement/max.test.js | 65 ++++++++++++++++++++++++++++++- Sprint-1/implement/sum.js | 19 +++++++++ Sprint-1/implement/sum.test.js | 21 +++++++++- Sprint-1/refactor/includes.js | 4 +- eslint.config.mjs | 9 +++++ mean.js | 1 + mean.test.js | 30 +++++++++++--- package.json | 24 ++++++++++++ swapfirstandlastarray.js | 13 +++++++ 15 files changed, 291 insertions(+), 16 deletions(-) create mode 100644 Doubleallnumber.js create mode 100644 Fail fast and succeed create mode 100644 eslint.config.mjs create mode 100644 package.json create mode 100644 swapfirstandlastarray.js diff --git a/Doubleallnumber.js b/Doubleallnumber.js new file mode 100644 index 000000000..c17c003dc --- /dev/null +++ b/Doubleallnumber.js @@ -0,0 +1,14 @@ +// Can you fix this code? +function doubleAllNumbers(myNums) { + let doubledNumbers= []; + + for (let n of myNums) { + doubledNumbers.push(n * 2); + } + + return doubledNumbers; +} + +const myNums = [10, 20, 30]; +doubleAllNumbers(myNums); +console.log(doubleAllNumbers(myNums)); \ No newline at end of file diff --git a/Fail fast and succeed b/Fail fast and succeed new file mode 100644 index 000000000..f7d7f0647 --- /dev/null +++ b/Fail fast and succeed @@ -0,0 +1,16 @@ +import { prependOnceListener } from "node:cluster" +import { timeStamp } from "node:console" + +Failure: + +Failure is not the end. It is the beginning of success. +For example, I have tried to paint a picture and gave it to them on the street. After they rejected several times, I knew that they were too busy to go to their office. I realized I need to find a place +where they will sit down. In the next time , I made a drawing on the train and gave it to people. They felt happy because +they were bored on the train. They felt suprised when someone drew them and the drawing makes their day. +Next time, my objective is to draw their face on the train because they feel close to their portaits and they feel relaxed when they sit down on the train. +Eventually, I succeeded to make drawings for many passengers. + + + + + diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..0a0c740c1 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -1,14 +1,38 @@ + // Fix this implementation // Start by running the tests for this function // If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory + // 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). + function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; + + if(!Array.isArray(list)){ + return null;} +const numbersOnly = list.filter(item => typeof item === 'number') + +if (numbersOnly.length===0){ + return null; +} +// if the arraylength is even, it will return the average of the two middle numbers +else if(numbersOnly.length%2===0){ +numbersOnly.sort((a, b) => a - b); + const middleIndex = Math.floor(numbersOnly.length / 2); + const median = (numbersOnly[middleIndex] + numbersOnly[middleIndex - 1]) / 2; return median; + } + +else if (numbersOnly.length%2===1){ + //if the a-b is negative, it means a is smaller than b, so it will be sorted to the left of b + numbersOnly.sort((a, b) => a - b); + const middleIndex = Math.floor(numbersOnly.length / 2); + const median = numbersOnly[middleIndex]; + return median; +} +} module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..3331a18b4 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,14 @@ -function dedupe() {} +function dedupe(list) { + let uniqueList = []; + // if it is duplicate insides the array, you take out the duplicate and return a new array with duplicates removed while preserving the first occurrence of each element from the original array. + + list.forEach(function (num) { + if (!uniqueList.includes(num)) { + uniqueList.push(num); + } + }); + + return uniqueList; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index d7c8e3d8e..7fc52844e 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -16,7 +16,6 @@ E.g. dedupe([1, 2, 1]) returns [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"); // Given an array with no duplicates // When passed to the dedupe function @@ -24,5 +23,29 @@ test.todo("given an empty array, it returns an empty array"); // Given an array of strings or numbers // When passed to the dedupe function -// Then it should return a new array with duplicates removed while preserving the +// Then it should return a new array with duplicates removed while preserving the // first occurrence of each element from the original array. + +// 1. Bring in your dedupe function (adjust the path if needed) + +describe("dedupe function", () => { + // Test Case 1: Array with no duplicates + test("should return a copy of the original array when passed no duplicates", () => { + const input = [1, 2, 3, 4]; + const result = dedupe(input); + + // .toEqual checks the INSIDE of the arrays, not just if they are the same box + expect(result).toEqual([1, 2, 3, 4]); + + // Optional: Verify it's a NEW array copy, not the exact same array in memory + expect(result).not.toBe(input); + }); + + // Test Case 2: Array with duplicates (Strings and Numbers) + test("should remove duplicates while keeping the first occurrence of each element", () => { + const input = ["cobble", "diamond", "cobble", "dirt", "diamond", 1, 2, 1]; + const expectedOutput = ["cobble", "diamond", "dirt", 1, 2]; + + expect(dedupe(input)).toEqual(expectedOutput); + }); +}); diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..f30ea1af2 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,25 @@ function findMax(elements) { + if (!Array.isArray(elements)) { + return Infinity; + } + + const onlyNumbers = elements.filter((item) => typeof item === "number"); + + if (onlyNumbers.length === 0) { + return Infinity; + } + + if (elements === "number") { + return elements; + } + + let Max = onlyNumbers[0]; + for (let i = 0; i < onlyNumbers.length; i++) { + if (onlyNumbers[i] > Max) { + Max = onlyNumbers[i]; + } + } + return Max; } module.exports = findMax; diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd88..69e23d1de 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -9,7 +9,6 @@ You should implement this function in max.js, and add tests for it in this file. We have set things up already so that this file can see your function from the other file. */ - const findMax = require("./max.js"); // Given an empty array @@ -18,6 +17,70 @@ const findMax = require("./max.js"); // Delete this test.todo and replace it with a test. test.todo("given an empty array, returns -Infinity"); +describe("findMax funtion", () => { + test("given an empty array, returns -Infinity", () => { + const input = []; + const result = findMax(input); + + expect(result).toEqual(Infinity); + + expect(result).not.toBe(input); + }); + + test("Given an array with one number ", () => { + const input = [1]; + const result = findMax(input); + + expect(result).toEqual(1); + + expect(result).not.toBe(input); + }); + + test("Given an array with both positive and negative numbers", () => { + const input = [1, -2]; + const result = findMax(input); + + expect(result).toEqual(1); + + expect(result).not.toBe(input); + }); + + test("Given an array with just negative numbers", () => { + const input = [-3, -2]; + const result = findMax(input); + + expect(result).toEqual(-2); + expect(result).not.toBe(input); + }); + + test(" Given an array with decimal numbers", () => { + const input = [1.23, 1.24]; + const result = findMax(input); + + expect(result).toEqual(1.24); + + expect(result).not.toBe(input); + }); + + test("Given an array with non-number values", () => { + const input = ["tiger", 1]; + const result = findMax(input); + + expect(result).toEqual(1); + + expect(result).not.toBe(input); + }); + + test("Given an array with only non-number values", () => { + const input = ["tiger", "bear"]; + const result = findMax(input); + + expect(result).toEqual(Infinity); + + expect(result).not.toBe("input"); + }); +}); + // Given an array with one number // When passed to the max function // Then it should return that number diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..6f35c6252 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,23 @@ function sum(elements) { + +if (!Array.isArray(elements)) { + return Infinity; + } + + const onlyNumbers = elements.filter((item) => typeof item === "number"); + + +let total= 0 + +for( const num of onlyNumbers ){ + total+= num ; } +return total; +} + + module.exports = sum; + + + diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090ca..694cea229 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -13,7 +13,26 @@ 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") + +describe("Given an empty array, returns 0", () => { + test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); + }); + + test("Given an array containing negative numbers", () => { + expect(sum([1, -2, 3])).toBe(2); + }); + + test("Given an array with decimal/float numbers", () => { + expect(sum([1, 2.2, 3])).toBe(6.2); + }); + test("Given an array containing non-number values", () => { + expect(sum([1, "China", 3])).toBe(4); + }); + test("Given an array with only non-number values", () => { + expect(sum(["Colommbia", "Turkey"])).toBe(0); + }); +}); // Given an array with just one number // When passed to the sum function diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..7032f6c59 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,13 +1,11 @@ // 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; } } return false; } - module.exports = includes; diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 000000000..d3da14396 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,9 @@ +import js from "@eslint/js"; +import globals from "globals"; +import pluginReact from "eslint-plugin-react"; +import { defineConfig } from "eslint/config"; + +export default defineConfig([ + { files: ["**/*.{js,mjs,cjs,jsx}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: globals.browser } }, + pluginReact.configs.flat.recommended, +]); diff --git a/mean.js b/mean.js index e69de29bb..8b1378917 100644 --- a/mean.js +++ b/mean.js @@ -0,0 +1 @@ + diff --git a/mean.test.js b/mean.test.js index dc5456112..1c2654326 100644 --- a/mean.test.js +++ b/mean.test.js @@ -1,7 +1,25 @@ -test("calculates the median of a list of odd length", () => { - const list = [10, 20, 30, 50, 60]; - const currentOutput = calculateMedian(list); - const targetOutput = 30; +function calculateMedian(list) { - expect(currentOutput).toEqual(targetOutput); -}); + if(list.length=== 0) return 0; + + + const sortedList = [...list].sort((a, b) => a-b); + + const middleIndex = Math.floor(sortedList.length /2 ); + + if (sortedList.length % 2 === 0) { + const leftMiddle = sortedList[middleIndex -1]; + const rightMiddle = sortedList[middleIndex]; + return (leftMiddle + rightMiddle) /2; +} else { + + return sortedList[middleIndex]; +} +} + +test("doesn't modify the input", () => { + const list = [1, 2, 4, 5]; + calculateMedian(list); + + expect(calculateMedian(list)).toEqual(3); // Note that the toEqual matcher checks the values inside arrays when comparing them - it doesn't use `===` on the arrays, we know that would always evaluate to false. +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 000000000..b4b318e46 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "module-data-groups", + "version": "1.0.0", + "description": "Like learning a musical instrument, programming requires daily practice.", + "main": "iteration.js", + "scripts": { + "test": "jest" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Motorcycle-lab/Module-Data-Groups.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "bugs": { + "url": "https://github.com/Motorcycle-lab/Module-Data-Groups/issues" + }, + "homepage": "https://github.com/Motorcycle-lab/Module-Data-Groups#readme", + "devDependencies": { + "jest": "^30.4.2" + } +} diff --git a/swapfirstandlastarray.js b/swapfirstandlastarray.js new file mode 100644 index 000000000..dcb5d1874 --- /dev/null +++ b/swapfirstandlastarray.js @@ -0,0 +1,13 @@ +function swapFirstAndLast(arr){ + +//swap first and the last elements of the array +const first = arr[0]; +const last = arr[arr.length - 1]; +arr[0] = last; +arr[arr.length - 1] = first; + +} + +const myArray = [5, 2, 3, 4, 1]; +swapFirstAndLast(myArray); +console.log(myArray); \ No newline at end of file From 7f47bff122165a206bd6d1328fccd4a6dd8f3031 Mon Sep 17 00:00:00 2001 From: Arthur <> Date: Sat, 11 Jul 2026 15:05:25 +0100 Subject: [PATCH 3/4] clear --- Sprint-2/debug/address.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..c4701e9fc 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,10 @@ const address = { postcode: "XYZ 123", }; + + + console.log(`My house number is ${address[0]}`); + + + From 4ecaeb30444e6dc6ba886eb57dab85d859965181 Mon Sep 17 00:00:00 2001 From: Arthur <> Date: Thu, 16 Jul 2026 14:35:16 +0100 Subject: [PATCH 4/4] Final commit of the app --- Sprint-3/package.json | 2 +- Sprint-3/quote-generator/index.html | 16 ++++++-- Sprint-3/quote-generator/package.json | 5 ++- Sprint-3/quote-generator/quotes.js | 31 ++++++++++++-- Sprint-3/quote-generator/style.css | 59 +++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 9 deletions(-) diff --git a/Sprint-3/package.json b/Sprint-3/package.json index 711a5390f..9f335009a 100644 --- a/Sprint-3/package.json +++ b/Sprint-3/package.json @@ -24,7 +24,7 @@ "homepage": "https://github.com/CodeYourFuture/Module-Data-Groups#readme", "devDependencies": { "@testing-library/dom": "^10.4.0", - "@testing-library/jest-dom": "^6.6.3", + "@testing-library/jest-dom": "^6.9.1", "@testing-library/user-event": "^14.6.1", "jest": "^30.0.4", "jest-environment-jsdom": "^30.0.4" diff --git a/Sprint-3/quote-generator/index.html b/Sprint-3/quote-generator/index.html index 30b434bcf..c868c216e 100644 --- a/Sprint-3/quote-generator/index.html +++ b/Sprint-3/quote-generator/index.html @@ -5,11 +5,21 @@