Skip to content

Glasgow| May-2025| Bassam Alshujaa| Sprint-2 | module-Data-Groups #652

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 3 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: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ const address = {
postcode: "XYZ 123",
};

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

// the code has an error which is address[0], this an object so we can't get it by index
6 changes: 4 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}

// we can't loop an object by for.. of loop when can loop using for.. in
10 changes: 7 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const recipe = {
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};
console.log(recipe.title);
console.log(`serves:${recipe.serves}`);
console.log(`ingredients:`);

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
for (let i of recipe.ingredients) {
console.log(i);
}
// We should loop recipe.ingredients to log them out separately
21 changes: 20 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
function contains() {}
function contains(obj,check) {
const out = {}
// console.log(obj)
if (Object.keys(obj).length ==0) {
console.log("Empty");
return false;
}
for (let i in obj){
if (obj[check]) {
console.log("Yes");
return true;
} else {
console.log("NO");
return false;
}
}
}
console.log(contains({} , []));



module.exports = contains;
22 changes: 21 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,36 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("contains on empty object returns false" , () =>{
const input = { } ;
const outPut = contains(input, 'a')
expect(outPut).toEqual(false)
});

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

test("contains with an existing property name returns true", () => {
const input = { a: 1, b: 2, s: 5 };
const outPut = contains(input, "a");
expect(outPut).toEqual(true);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("contains with a non-existent property name returns false", () => {
const input = { a: 1, b: 2, s: 5 };
const outPut = contains(input, "l");
expect(outPut).toEqual(false);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error

test("contains invalid parameters like an array returns false", () => {
const input = 'abx';
const outPut = contains(input, "p");
expect(outPut).toEqual(false);
});
11 changes: 9 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function createLookup() {
// implementation here
function createLookup(obj) {
if (!Array.isArray(obj) || !obj.every(Array.isArray)) {
throw new Error("Input must be an array of arrays");
}
const cur = {};
for (let list of obj) {
cur[list[0]] = list[1];
}
return cur;
}

module.exports = createLookup;
20 changes: 18 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
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 input = createLookup([
["US", "USD"],
["CA", "CAD"],
]);
const outPut = { US: "USD", CA: "CAD" };
expect(input).toEqual(outPut);
});

Choose a reason for hiding this comment

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

You could also consider testing the cases where invalid input is given, if you add validation checks to lookup,js

test("creates a lookup from multiple pairs", () => {
const input = [
["US", "USD"],
["CA", "CAD"],
["JP", "JPY"],
];
const expected = { US: "USD", CA: "CAD", JP: "JPY" };
expect(createLookup(input)).toEqual(expected);
});
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down
15 changes: 10 additions & 5 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ function parseQueryString(queryString) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
for (let pair = 0; pair < keyValuePairs.length; pair++) {
const index = keyValuePairs[pair].indexOf("=");
if (index === -1) {
queryParams[keyValuePairs[pair]] = "";
} else {
const key = keyValuePairs[pair].slice(0, index);
const value = keyValuePairs[pair].slice(index + 1);
queryParams[key] = value;
}
}

return queryParams;
}
parseQueryString("equation=x=y+1");

module.exports = parseQueryString;
16 changes: 13 additions & 3 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
const parseQueryString = require("./querystring.js")

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

test("parses multiple key-value pairs", () => {
expect(parseQueryString("a=1&b=2&c=3")).toEqual({ a: "1", b: "2", c: "3" });
});

test("handles key with empty value", () => {
expect(parseQueryString("key=")).toEqual({ key: "" });
});

test("takes the last value for duplicate keys", () => {
expect(parseQueryString("a=1&a=2&a=3")).toEqual({ a: "3" });
});
19 changes: 18 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function tally() {}
function tally(list) {
if (!Array.isArray(list)) {
throw new Error ("Input must be an array");
} else {
const result = {};
if (list) {
for (const item of list) {
if (result[item]) {
result[item] += 1;
} else {
result[item] = 1;
}
}
}
return result;
}
}
console.log(tally());

module.exports = tally;
17 changes: 15 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,25 @@ const tally = require("./tally.js");
// Given an empty array
// When passed to tally
// Then it should return an empty object
test.todo("tally on an empty array returns an empty object");
test("tally on an empty array returns an empty object", () => {
const input = "";
const outPut = {};
expect(tally(input)).toEqual(outPut);
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

test("tally counts duplicates correctly", () => {
const input = ["a", "b", "a", "c", "b"];
const output = { a: 2, b: 2, c: 1 };
expect(tally(input)).toEqual(output);
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws error on invalid input", () => {
const input = 'ab';
const output = "Input must be an array";
expect(() =>tally(input)).toThrow(output);
});
1 change: 1 addition & 0 deletions Sprint-2/implement/tempCodeRunnerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
equation=x=y+1
12 changes: 10 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
invertedObj[value] = key;
}

return invertedObj;
}
console.log(invert({ s: 1, c: 2 , p:5}));

// a) What is the current return value when invert is called with { a : 1 }
// the current value is { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// the current value is { key: 2 }

// c) What is the target return value when invert is called with {a : 1, b: 2}
// { '1': 'a', '2': 'b' }

// c) What does Object.entries return? Why is it needed in this program?
// it returns one array containing smaller arrays — each one holds key and value,
// and this lets us access the keys and values easily by looping the array

// d) Explain why the current return value is different from the target output
// because it returns a key that is called key not the key in the obj and it equals the value which gives {key: value}

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// console.log(invert({ a: 1, b: 2 })); returns { '1': 'a', '2': 'b' }
// console.log(invert({ s: 1, c: 2, p: 5 })); returns { '1': 's', '2': 'c', '5': 'p' }
6 changes: 6 additions & 0 deletions package-lock.json

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