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

NW6 | Fikret Ellek | JS-2-Module | Week-1 #183

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
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.

10 changes: 9 additions & 1 deletion week-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
// 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 sortedList = [...list].sort((a, b) => a - b);
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
let median = 0;

if (list.length % 2 === 1) {
median = sortedList.splice(middleIndex, 1)[0];
} else {
median = (sortedList[middleIndex] + sortedList[middleIndex - 1]) / 2;
}

return median;

Choose a reason for hiding this comment

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

Nice work!

}

Expand Down
16 changes: 15 additions & 1 deletion week-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(list) {
if (list.length === 0) {
return list;
} else {
let dedupedList = [];
for (let x of list) {

Choose a reason for hiding this comment

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

Can you please rename the x variable with a meaningful name?

if (!dedupedList.includes(x)) {
dedupedList.push(x);
}
}
return dedupedList;
}
}

module.exports = dedupe;
21 changes: 21 additions & 0 deletions week-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ test.todo("given an empty array, it returns an empty array");
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values

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

test("given an array with no duplicates, it should return a copy of the original array", () => {
expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
expect(dedupe([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]);
});

test("given an array with strings or numbers, it should remove the duplicate values", () => {
expect(dedupe([1, 2, 3, 4, 5, 5, 5, 5])).toEqual([1, 2, 3, 4, 5]);
expect(dedupe(["ethem", "ethem", "mark", "mark", "jolie"])).toEqual([
"ethem",
"mark",
"jolie",
]);
expect(dedupe([1, 1, 1, "ethem", "ethem"])).toEqual([1, "ethem"]);
});
});
11 changes: 11 additions & 0 deletions week-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function maxNum(list) {
let maximum = -Infinity;
for (let x of list) {
if (x > maximum && !x.isNaN) {

Choose a reason for hiding this comment

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

Also, x is not really useful you should make all your functions and variables more meaningful.

maximum = x;
}
}
return maximum;
}

module.exports = maxNum;
52 changes: 33 additions & 19 deletions week-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,38 @@ 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 maxNum = 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");

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

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

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

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
describe("maxNum", () => {
// Given an empty array
// When passed to the max function
// Then it should return -Infinity
test("given an empty array, returns -Infinity", () => {
expect(maxNum([])).toEqual(-Infinity);
});
// Given an array with one number
// When passed to the max function
// Then it should return that number
test("given an empty array, returns -Infinity", () => {
expect(maxNum([1])).toEqual(1);
});
// 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 empty array, returns -Infinity", () => {
expect(maxNum([1, 2, -7, 5, -3, 3, 0])).toEqual(5);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("given an empty array, returns -Infinity", () => {
expect(maxNum([1.5, 6.8, 6.9, 6.90000001, 6.90001, 5])).toEqual(6.90001);
});
// 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 empty array, returns -Infinity", () => {
expect(maxNum(["a", 1, "b", "m", [1, 6, 7, 8, "a"], "b"])).toEqual(1);
});
});
15 changes: 15 additions & 0 deletions week-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function sum(list) {
if (list.length === 0) {
return 0;
} else {
let sumOfNum = 0;

Choose a reason for hiding this comment

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

Is good practice to declare your variables at the top at the beginning of the function. SO can you remove the sumOfNum var to line 2?

for (let x of list) {
if (!isNaN(x)) {
sumOfNum += x;
}
}
return sumOfNum;
}
}

module.exports = sum;
53 changes: 34 additions & 19 deletions week-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,39 @@ 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)
*/

// Acceptance Criteria:

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

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
const sum = require("./sum.js");

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

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

// 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
describe("sum() sums the numerical elements of an array", () => {
// Given an empty array
// When passed to the sum function
// Then it should return 0
test("Given an empty array, Then it should return 0", () => {
expect(sum([])).toEqual(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, Then it should return that number", () => {
expect(sum([1])).toEqual(1);
});
// 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 containing negative numbers, Then it should still return the correct total sum", () => {
expect(sum([-1, -2, -3])).toEqual(-6);
});
// 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/float numbers, Then it should return the correct total sum", () => {
expect(sum([1.5, 3.55])).toEqual(5.05);
});
// 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 containing non-number values, Then it should ignore the non-numerical values and return the sum of the numerical elements", () => {
expect(sum([1, 2, "a", "b", "c", 3])).toEqual(6);
});
});
6 changes: 3 additions & 3 deletions week-1/refactor/find.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// 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 (let x of list) {
const element = x;
if (element === target) {
return index;
return list.indexOf(x);
}
}
return -1;
Expand Down
23 changes: 23 additions & 0 deletions week-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("fs");
const txtDoc = fs.readFileSync(
"/home/eytha/cyf/Module-JS2/week-1/stretch/aoc-2018-day1/input.txt",
"utf8"
);
const txtList = txtDoc.split("\n");
const numList = txtList.map(Number);

function calculateFrequency(list) {
let sum = 0;

for (let x of list) {
if (!isNaN(x)) {
sum += x;
}
}
return sum;
}

console.log(calculateFrequency(numList));
console.log("Current working directory:", process.cwd());

module.exports = calculateFrequency;
34 changes: 34 additions & 0 deletions week-1/stretch/aoc-2018-day1/solution.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const calculateFrequency = require("./solution");

describe("testing the calculateFrequency function", () => {
test("testing with an empty array", () => {
const input = [];
const expectedOutput = calculateFrequency(input);
const targetOutput = 0;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with positive numbers", () => {
const input = [1, 2, 3, 4];
const expectedOutput = calculateFrequency(input);
const targetOutput = 10;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with negative numbers", () => {
const input = [-1, -2, -3, -4];
const expectedOutput = calculateFrequency(input);
const targetOutput = -10;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with positive and negative numbers", () => {
const input = [-1, 2, 3, -4];
const expectedOutput = calculateFrequency(input);
const targetOutput = 0;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with characters", () => {
const input = ["a", "b", "c", "d"];
const expectedOutput = calculateFrequency(input);
const targetOutput = 0;
expect(expectedOutput).toEqual(targetOutput);
});
});