Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

NW6 | Rabia Avci | JS2 Module | [TECH ED]Complete week 2 exercises | Week 2 #231

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions week-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
// If you're in the week-1 directory, you can run npm test -- fix to run the tests in the fix directory

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
const middleIndex = (list.length + 1)/2;

if(list.length%2 !== 0){
return list[middleIndex-1];
}
else{
const firstIndex = Math.floor(middleIndex);
const secondIndex = Math.round(middleIndex);

return (list[firstIndex-1]+list[secondIndex-1])/2;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great 👍


}
}

module.exports = calculateMedian;
module.exports = calculateMedian;
50 changes: 49 additions & 1 deletion week-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
function dedupe() {}
//manual check
// function dedupe(input) {

// let output = [];

// for(let i=0; i<input.length; i++){
// let duplicate = false;

// for (let j = 0; j < output.length; j++) {
// if (input[i] === output[j]) {
// duplicate = true;
// }
// }

// if (!duplicate) {
// output.push(input[i]);
// }
// }

// return output;
// }


function dedupe(input) {

let output = [];

for(let i=0; i<input.length; i++){

if (!output.includes(input[i])) {
output.push(input[i]);
}
}

return output;
}












module.exports = dedupe;
15 changes: 14 additions & 1 deletion week-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
// 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");

test("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toStrictEqual([]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array

test("given an array with no duplicates, it returns a copy of the original array", () => {
expect(dedupe([5, 1, 2, 3, 8])).toStrictEqual([5, 1, 2, 3, 8]);
});


// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values


test("given an array with strings or numbers, it removes the duplicate values", () => {
expect(dedupe([5, 1, 'a', 'a', 2, 'b', 3, 3, 8, 8])).toStrictEqual([5, 1, 'a', 2,'b', 3, 8]);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

39 changes: 39 additions & 0 deletions week-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Manual check
// function max(input){
// if(input.length == 0){
// return -Infinity;
// }

// if(input.length == 1){
// return input[0];
// }
// let maxNum = input[0];
// for (let i=0; i<input.length; i++){
// if(typeof input[i] !== 'string'){
// maxNum = input[i]
// break;
// }
// }

// for (let i=1; i<input.length; i++){
// if(input[i] > maxNum){
// maxNum = input[i];
// }
// }
// return maxNum;
// }


function max(input){
let output = [];
for(let i=0; i<input.length; i++){
if (typeof input[i] !== 'string') {
output.push(input[i]);
}
}
return Math.max(...output);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing, don't forget to tidy commented out code :)

}



module.exports = max;
23 changes: 22 additions & 1 deletion week-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,45 @@ In this kata, you will need to implement a function that sums the numerical elem
E.g. max([30, 50, 10, 40]), target output: 50
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)
*/
const max = require("./max.js");

// Given an empty array
// When passed to the max function
// Then it should return -Infinity
test.todo("given an empty array, returns -Infinity");

test("given an empty array, returns -Infinity", () => {
expect(max([])).toBe(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number

test("given an array with one number, returns number", () => {
expect(max([2])).toBe(2);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test("given an array with both positive and negative number, returns max", () => {
expect(max([-7, 6, 4, -3])).toBe(6);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("given an array with decimal number, returns max", () => {
expect(max([7.6, 1.6, 9.4, 3.4])).toBe(9.4);
});

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values


test("given an array with non-number values, returns max, ignore non-numeric values", () => {
expect(max(["17", 12, "c", 5.4])).toBe(12);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

20 changes: 20 additions & 0 deletions week-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

function sum(input) {
let output = [];
for (let i = 0; i < input.length; i++) {
if (typeof input[i] !== "string") {
output.push(input[i]);
}
}
let total = 0;
for (let i = 0; i < output.length; i++){
total = total + output[i];
}
return total;
}





module.exports = sum;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

23 changes: 23 additions & 0 deletions week-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,48 @@ In this kata, you will need to implement a function that sums the numerical elem
E.g. sum([10, 20, 30]), target output: 60
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
*/
const sum = require("./sum.js");

// Acceptance Criteria:

// Given an empty array
// When passed to the sum function
// Then it should return 0

test("given an empty array, returns 0 ", () => {
expect(sum([])).toBe(0);
});


// Given an array with just one number
// When passed to the sum function
// Then it should return that number

test("given an array with just one number, returns that number ", () => {
expect(sum([12])).toBe(12);
});


// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

test("given an array with both positive and negative number, returns correct total sum ", () => {
expect(sum([-7, 6, 4, -3])).toBe(0);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

test("given an array with decimal number, returns the correct total sum", () => {
expect(sum([7.6, 1.6, 9.4, 3.4])).toBe(22);
});

// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values and return the sum of the numerical elements

test("given an array with non-number values, returns the sum of the numerical elements, ignore non-numeric values", () => {
expect(sum(["17", 12, "c", 5.4])).toBe(17.4);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

5 changes: 2 additions & 3 deletions week-1/refactor/find.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Refactor the implementation of find to use a for...of loop

function find(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
for (const element of list){
if (element === target) {
return index;
return list.indexOf(element);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid refactoring 👍

}
}
return -1;
Expand Down
6 changes: 5 additions & 1 deletion week-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// Address is an object, and properties are accessed using their names, not numerical indices.

// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working
Expand All @@ -12,4 +14,6 @@ const address = {
postcode: "XYZ 123",
};

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


10 changes: 7 additions & 3 deletions week-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

// for...of loop is designed to iterate over iterable objects like arrays, not over plain objects. we need to use for .. in or Object.values() with for of
// This program attempts to log out all the property values in the object.
// But it isn't working. Explain why first and then fix the problem

Expand All @@ -11,6 +11,10 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const element of Object.values(author)){
console.log(element);
}

for (const element in author) {
console.log(author[element]);
}
7 changes: 5 additions & 2 deletions week-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);

for (const element of recipe.ingredients) {
console.log(element);
}
7 changes: 6 additions & 1 deletion week-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}
function contains(obj, key) {
if (typeof obj === "object" && Object.hasOwnProperty.call(obj, key)) {
return true;
}
return false;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


module.exports = contains;
Loading