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/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]}`); + + + 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 @@