Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doubleallnumber.js
Original file line number Diff line number Diff line change
@@ -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));
16 changes: 16 additions & 0 deletions Fail fast and succeed
Original file line number Diff line number Diff line change
@@ -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.





28 changes: 26 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 14 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -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;
27 changes: 25 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,36 @@ 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
// Then it should return a copy of the original 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);
});
});
21 changes: 21 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -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;
65 changes: 64 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -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;



21 changes: 20 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ const address = {
postcode: "XYZ 123",
};




console.log(`My house number is ${address[0]}`);



2 changes: 1 addition & 1 deletion Sprint-3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 13 additions & 3 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<script defer src="quotes.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>hello there</h1>
<p id="quote"></p>
<p id="author"></p>
<div class="card">
<div class="quote-container">

<span class="quote-mark">“</span><p id="quote"></p>
<p id="author"></p> </div>

<div class="button-container">

<button type="button" id="new-quote">New quote</button>
</div>
</body>

</html>


5 changes: 4 additions & 1 deletion Sprint-3/quote-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Loading
Loading