Skip to content

Sheffield|May-2025|Sheida Shabankari|Module-Data -Groups| Sprint-2 #574

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

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
6 changes: 3 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const author = {
age: 40,
alive: true,
};

for (const value of author) {
console.log(value);
//console.log(author);
for (const key in author) {
console.log(author[key]);
}
8 changes: 5 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`);
console.log("ingredients:");
for(const item of recipe.ingredients){
console.log(item);
}
11 changes: 10 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function contains() {}
function contains(inputObject,inputProperty) {
if (
inputObject === null ||
typeof inputObject !== "object" ||
Array.isArray(inputObject)
) {
return false;
}
return inputObject.hasOwnProperty(inputProperty);
}

module.exports = contains;
29 changes: 28 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,43 @@ 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 obj = {};
const result = contains(obj, "a");
expect(result).toBe(false);
});


// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("object contains an existing property name", () => {
const obj = { a: 1, b: 2 };
const result = contains(obj, "a");
expect(result).toBe(true);
});


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

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("Given invalid parameters like an array",() => {
const obj = ["a",1,"b", 2 ];
const result = contains(obj, "a");
expect(result).toBe(false);
});
test("Given invalid parameters like an array2", () => {
const obj = ["a", 1, "b", 2];
const result = contains(obj, "1");
expect(result).toBe(false);
});
15 changes: 12 additions & 3 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function createLookup() {
// implementation here

function createLookup(countryCurrArray) {
if (
!Array.isArray(countryCurrArray) ||
!countryCurrArray.every((item) => Array.isArray(item))
) {
return null;
}
return Object.fromEntries(countryCurrArray);


}

module.exports = createLookup;
module.exports = createLookup;
45 changes: 44 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
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 array = [
["US", "USD"],
["CA", "CAD"],
["GB", "GBP"],
];
const result = createLookup(array);
expect(result).toEqual({
US: "USD",
CA: "CAD",
GB: "GBP",
});
}
);

test("input array is empty", () => {
const array = [];
const result = createLookup(array);
expect(result).toEqual({});
});

test("The input is an array but doesn't contain array elements", () => {
const array = [1,2,3,4];
const result = createLookup(array);
expect(result).toEqual(null);
});

test("ُThe input is not an array", () => {
const array ="US-USD" ;
const result = createLookup(array);
expect(result).toEqual(null);
});

test("ُThe input is an array of strings", () => {
const array = ["US:USD","CA:CAD"];
const result = createLookup(array);
expect(result).toEqual(null);
});

test("creates a country currency code lookup for one array", () => {
const array = [["US", "USD"]];
const result = createLookup(array);
expect(result).toEqual({US: "USD"});
});

/*

Expand Down
5 changes: 3 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
const [key, ...value] = pair.split("=");
const strvalue = value.join("=");
queryParams[key] = strvalue;
}

return queryParams;
Expand Down
25 changes: 25 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,28 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

test("parses querystring values contains one pair", () => {
expect(parseQueryString("brand=Tesla")).toEqual({
brand: "Tesla",
});
});

test("parses querystring contains more than one pair ",() =>{
expect(parseQueryString("sort=lowest&colour=yellow")).toEqual({
"sort":"lowest",
"colour":"yellow"});
});

test("parses empty querystring", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses key without value", () => {
expect(parseQueryString("name")).toEqual({name:""});
});


test("parses key with empty value", () => {
expect(parseQueryString("name=")).toEqual({ name: "" });
});
22 changes: 21 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
function tally() {}
function tally(itemArray) {
if (!Array.isArray(itemArray)) {
throw new Error("invalid input");
}

if (itemArray.length == 0 ) {
return {};
}

let countArray={};
for(const item of itemArray ){
if(countArray.hasOwnProperty(item)){
countArray[item]++;
}else{
countArray[item]=1;
}
}
return countArray;
}


module.exports = tally;

24 changes: 23 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,34 @@ 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",()=>{
expect(tally([])).toEqual({});});


// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test(
"tally on an array with duplicate items returns counts for each unique item",()=>{
expect(tally(["a", "a", "a","b"])).toEqual({a:3,b:1});

}
);

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally on an invalid input like a string should throw an error",()=>{
expect(()=>tally("String Test")).toThrow("invalid input")
});

test("tally on an invalid input like null should throw an error",()=>{
expect(()=>tally()).toThrow("invalid input")
});


test("tally on an array with various input", () => {
expect(tally(["a"])).toEqual({ a: 1 });
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});

13 changes: 11 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}
module.exports = invert;

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

//{key : 1} because invertedObj.key= value add a property with the exact name "key" and value "1" to invertedObj.

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

// c) What is the target return value when invert is called with {a : 1, b: 2}
//{key : 2} because first the function add {key:1} to invertedObj and then modify the property and assign value 2 to key "key".

// 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 pairs of keys and values as an Array of pairs.

// d) Explain why the current return value is different from the target output

//Because invertedObj.key means object "invertedObj" has a key property with the exact name "key" and we need to add a key that its name is the value of key property.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
9 changes: 9 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const invert = require("./invert.js");

test("invert an object with one pair",()=>{
expect(invert({a:1})).toEqual({1:"a"});
});

test("invert an object with more than ne pair", () => {
expect(invert({ a: 1 , b : 2})).toEqual({ 1: "a" , 2: "b"});
});
18 changes: 17 additions & 1 deletion Sprint-2/package-lock.json

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

3 changes: 2 additions & 1 deletion Sprint-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^29.7.0"
"jest": "^29.7.0",
"prettier": "^3.6.2"
}
}