Skip to content

London|Haftamu Kebedew|Module-Data-Groups|Sprint 2|Exercise 2|Week 2 #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2b6a100
median files updated by fixing the implementation calculaltion
HaftamuKebedew Dec 2, 2024
1dbc52c
dedupe file updated
HaftamuKebedew Dec 2, 2024
4ac85d1
max file updated
HaftamuKebedew Dec 7, 2024
f33a91d
sum function defined
HaftamuKebedew Dec 7, 2024
dc22dd0
sum test case defined
HaftamuKebedew Dec 7, 2024
3ee4cb2
for loop refactored
HaftamuKebedew Dec 7, 2024
42ee168
calculatedFrequency function defined to import an input from input.tx…
HaftamuKebedew Dec 7, 2024
2018f5d
accessing houseNumber value updated
HaftamuKebedew Dec 7, 2024
8466b0e
for loop updated form of to in and accessed using their key when they…
HaftamuKebedew Dec 7, 2024
ccf47b2
explanation added in address file
HaftamuKebedew Dec 7, 2024
7e850d0
some predict and explanation added in author file
HaftamuKebedew Dec 7, 2024
3465d4f
recipe object of ingredients key value accessing way updated
HaftamuKebedew Dec 7, 2024
c1a784b
containes file functions modified
HaftamuKebedew Dec 7, 2024
d6193f6
contains test case defined
HaftamuKebedew Dec 7, 2024
3c0bb1a
lookup function updated
HaftamuKebedew Dec 7, 2024
c952987
one lookup taste case defined
HaftamuKebedew Dec 7, 2024
5240f46
test case added
HaftamuKebedew Dec 7, 2024
2433669
more test case added
HaftamuKebedew Dec 7, 2024
d4a8b8b
total function modified
HaftamuKebedew Dec 7, 2024
72196ca
more test case given
HaftamuKebedew Dec 7, 2024
d843fed
invert function defined and new invert test function created
HaftamuKebedew Dec 7, 2024
d898e3a
more test cases of invert defined
HaftamuKebedew Dec 7, 2024
b984ad0
count words function defined
HaftamuKebedew Dec 7, 2024
9dda208
countFrequencies and findMode defined in mode files and invoked in …
HaftamuKebedew Dec 7, 2024
4e2162f
till function and test updated
HaftamuKebedew Dec 7, 2024
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
2 changes: 1 addition & 1 deletion Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

Expand Down
4 changes: 2 additions & 2 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe("calculateMedian", () => {
});

test("returns the average of middle values for even length array", () => {
expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5);
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5);
expect(calculateMedian([1, 2, 3, 4])).toEqual(3);
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(4);
});

test("doesn't modify the input", () => {
Expand Down
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(arr) {
const result = [];
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (!result.includes(item))
result.push(item);

}

return result;
}
module.experts=dedupe
19 changes: 16 additions & 3 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@ E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) target output: [1, 2]
*/

describe("dedupe", () => {
// Acceptance Criteria:

// 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.todo("given an empty array, it returns an empty array");
test("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

// 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([1, 2, 3])).toEqual([1, 2, 3]);
expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element

test("given an array with duplicates, it removes duplicates and preserves first occurrence", () => {
// expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
})
14 changes: 14 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function findMax(elements) {
if (!Array.isArray(elements)) {
throw new TypeError("Input must be an array.");
}

let max = -Infinity;

for (let h = 0; h < elements.length; h++) {
const current = elements[h];
if (typeof current === 'number' && current > max) {
max = current;
}
}

return max;
}

module.exports = findMax;
27 changes: 21 additions & 6 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,43 @@ const findMax = require("./max.js");
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).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 that number", () => {
expect(findMax([22])).toBe(22);
});
// 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 positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-10, 30, -30, 15])).toBe(30);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("given an array with just negative numbers, returns the closest one to zero", () => {
expect(findMax([-10, -2, -7, -50])).toBe(-2);
});
// 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 numbers, returns the largest decimal number", () => {
expect(findMax([6.6, 2.1, 4.5, 4.2,7.1,1.9])).toBe(7.1);
});
// 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 the max ignoring non-numeric values", () => {
expect(findMax(['h', 70, null, 20, 'k'])).toBe(70);
});
// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
test("given an array with only non-number values, returns -Infinity", () => {
expect(findMax(['hello', true, undefined, 'world'])).toBe(-Infinity);
});
14 changes: 14 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function sum(elements) {
if (!Array.isArray(elements)) {
throw new TypeError("Input must be an array.");
}

let total = 0;

for (let h = 0; h < elements.length; h++) {
const current = elements[h];
if (typeof current === 'number') {
total += current;
}
}

return total;
}

module.exports = sum;
22 changes: 17 additions & 5 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,36 @@ 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")
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([3])).toBe(3);
});
// 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, returns the correct total sum", () => {
expect(sum([-10, 70, -5])).toBe(55);
});
// 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, returns the correct total sum", () => {
expect(sum([1.5, 2.5, 3.5,4.5,5.5])).toBe(17.5);
});
// 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, ignores non-numerical values and returns the sum of numerical elements", () => {
expect(sum(['a', 60, null, 20, 'b'])).toBe(80);
});
// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
test("given an array with only non-number values, returns 0", () => {
expect(sum(['bye', true, undefined, 'friend'])).toBe(0);
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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;
}
Expand Down
24 changes: 24 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs');
const path = require('path');
// Function to calculate the resulting frequency
const calculateFrequency = (changes) => {
let frequency = 0; // Start at zero
for (let change of changes)
frequency += parseInt(change, 10);
return frequency;
};

const inputFilePath = path.join(__dirname, 'input.txt');
let puzzleInput;

try {
const fileContent = fs.readFileSync(inputFilePath, 'utf-8');
puzzleInput = fileContent.split(/\r?\n/).filter(line => line.trim() !== '');
} catch (error) {
console.error('Error reading input file:', error);
process.exit(1);
}

// Calculate and log the resulting frequency
const result = calculateFrequency(puzzleInput);
console.log('Resulting frequency:', result);
10 changes: 9 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ const address = {
country: "England",
postcode: "XYZ 123",
};
/*
// we can use two ways to access and object value using their key
1.Dot notation: address.houseNumber
2.Bracket notation: address["houseNumber"]

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


// This is an array accessing but the given value is an objet so we have to use abject accessing methodes
console.log(`My house number is ${address.houseNumber}`);
8 changes: 5 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const author = {
age: 40,
alive: true,
};

for (const value of author) {
console.log(value);
/*
This causes an error because it does not know iterate over an object directly with for of loop. To iterate over an object's properties and their values, we can use
for in loop, This will iterate over the keys of the object. */
for (const key in author) {
console.log(author[key]);
}
6 changes: 5 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join('\n')}`);
/*

attempts to print the entire recipe object, recipe.ingredients.join('\n') takes the array and joins its elements with a newline character (\n), so each ingredient appears on a new line.
*/
6 changes: 5 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function contains() {}
function contains(obj,key) {
if (typeof obj !== 'object' || obj === null)
return false;
return key in obj;
}

module.exports = contains;
23 changes: 19 additions & 4 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,35 @@ as the object doesn't contains a key of 'c'
// Given a contains function
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

test("contains on empty object returns false", () => {
expect(contains({}, 'h')).toBe(false);
});
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("contains with an existing property returns true", () => {
expect(contains({h: 1, k: 2}, 'h')).toBe(true);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

test("contains with a non-existent property returns false", () => {
expect(contains({h: 1, k: 2}, 'b')).toBe(false);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("contains on an array returns false", () => {
expect(contains([], 'k')).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains on invalid parameters returns false", () => {
expect(contains(123, 'h')).toBe(false); // number
expect(contains('string', 'h')).toBe(false); // string
expect(contains(null, 'h')).toBe(false); // null
});

10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(countryCurrency) {
const lookup = {};

for (const pair of countryCurrency) {
const [countryCode, currencyCode] = pair;
lookup[countryCode] = currencyCode;
}
return lookup
}

module.exports = createLookup;
9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
test("creates a country currency code lookup for multiple codes", () => {
const countryCurrencyPairs = [['US', 'USD'], ['CA', 'CAD']];

const result = createLookup(countryCurrencyPairs);
expect(result).toEqual({
'US': 'USD',
'CA': 'CAD',
});})

/*

Expand Down
22 changes: 21 additions & 1 deletion Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@

const parseQueryString = require("./querystring.js")



test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
"equation": "x",
});
});

test("parses multiple key-value pairs", () => {
expect(parseQueryString("key1=value1&key2=value2")).toEqual({
"key1": "value1",
"key2": "value2",
});
});

test("parses key with no value", () => {
expect(parseQueryString("key1=")).toEqual({
"key1": "",
});
});
test("parses basic key-value pair", () => {
expect(parseQueryString("key=value")).toEqual({
"key": "value",
});
});
Loading