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 @@ Title here + -

hello there

-

-

+
+
+ +

+

+ +
+ +
+ + + diff --git a/Sprint-3/quote-generator/package.json b/Sprint-3/quote-generator/package.json index 0f6f98917..7b6cd6ea5 100644 --- a/Sprint-3/quote-generator/package.json +++ b/Sprint-3/quote-generator/package.json @@ -13,5 +13,8 @@ "bugs": { "url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues" }, - "homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme" + "homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme", + "devDependencies": { + "jest-environment-jsdom": "^30.4.1" + } } diff --git a/Sprint-3/quote-generator/quotes.js b/Sprint-3/quote-generator/quotes.js index 4a4d04b72..2bfba74b4 100644 --- a/Sprint-3/quote-generator/quotes.js +++ b/Sprint-3/quote-generator/quotes.js @@ -1,24 +1,42 @@ // DO NOT EDIT BELOW HERE - +// // pickFromArray is a function which will return one item, at // random, from the given array. -// + // Parameters // ---------- // choices: an array of items to pick from. // + // Returns // ------- // One item at random from the given array. // + // Examples of use // --------------- // pickFromArray(['a','b','c','d']) // maybe returns 'c' // You don't need to change this function -function pickFromArray(choices) { - return choices[Math.floor(Math.random() * choices.length)]; + +function pickFromArray(quotes) { + return quotes[Math.floor(Math.random() * quotes.length)]; } +// First condition: When a person click the button, it should generate back a quote; + +function updateWebpageWithQuote() { + const elem = document.querySelector("#quote"); + const elem2 = document.querySelector("#author"); + + // store into the variable + const elem3 = pickFromArray(quotes); + elem.innerText = elem3.quote; + elem2.innerText = elem3.author; +} + +// Second condition: when a person enter the website, it should have a quote appear; + +// Third condition: when a person clicks the button each time, it should have different quote. . // A list of quotes you can use in your app. // DO NOT modify this array, otherwise the tests may break! @@ -490,4 +508,9 @@ const quotes = [ }, ]; +const buttonElem = document.querySelector("#new-quote"); + +buttonElem.addEventListener("click", updateWebpageWithQuote); +updateWebpageWithQuote(); + // call pickFromArray with the quotes array to check you get a random quote diff --git a/Sprint-3/quote-generator/style.css b/Sprint-3/quote-generator/style.css index 63cedf2d2..b7df80c3d 100644 --- a/Sprint-3/quote-generator/style.css +++ b/Sprint-3/quote-generator/style.css @@ -1 +1,60 @@ /** Write your CSS in here **/ + +body{ + +background-color:orange; +margin:0; +display: flex; +min-height: 100vh; +justify-content: center; +align-items:center; +} + + +.card{ +background-color: white; +padding:30px; +border-radius: 8px; +max-width: 500px; + +} + +.quote-container{ +display: flex; +align-items: flex-start; +gap: 15px; +} + +.quote-mark{ +font-size: 90px; +color: orange; +line-height: 1; +font-family: sans-serif; + +} + + +#quote{ + color: orange; + font-size: 24px; +} + +#author{ + color: orange; + font-size: 24px; +} + +.button-container{ +display:flex; +justify-content:flex-end; +min-height: 50px; + +} + +#new-quote{ +background-color: orange; +color:white; +border: none; +} + + 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/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..8b1378917 --- /dev/null +++ b/mean.js @@ -0,0 +1 @@ + diff --git a/mean.test.js b/mean.test.js new file mode 100644 index 000000000..1c2654326 --- /dev/null +++ b/mean.test.js @@ -0,0 +1,25 @@ +function calculateMedian(list) { + + 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