Skip to content

London | 25-ITP- May | Fatima Z Belkedari | Sprint 2 Work Submission #637

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 16 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
@@ -1,5 +1,7 @@
// Predict and explain first...

// The code attempts to log out the houseNumber using[0] which is index. In this case, it will return undefined. Because
// values in the object are not accessed by index but by property name.
// 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,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}.`);
8 changes: 4 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

// Predict and explain first...For loop is generally used with arrays or iterable objects.However, in this case , author is an object
// it will not work directly with author
// 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,6 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const [key, value] of Object.entries(author)){
console.log(`${key} : ${value}`);
}
8 changes: 5 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

// using the ${recipe} here is incorrect because it is trying to log out the entire recipe as a string
// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
// How can you fix it?
Expand All @@ -11,5 +11,7 @@ const recipe = {
};

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

return key in obj;
}

module.exports = contains;
18 changes: 17 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,36 @@ 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("given a contains function, it should return true if the object contains the property, false otherwise", ()=>{
expect(contains({name:"Farah", age:30}, 'age')).toBe(true);
expect(contains({city:"London",job:"web developer"}, 'age')).toBe(false);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("given an empty object, it should return false", ()=>{
expect(contains({}, '')).toBe(false);
})

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

test("given an object with properties, it should return true with an existing property name",()=>{
expect(contains({city:"Manchester", country:"UK"}, 'city')).toBe(true);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("given an object with properties, it should return false with a non-existent property name",()=>{
expect(contains({firstName:"Farah", lastName:"BLK"}, 'age')).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, it should return false or throw an error",()=>{
expect(contains([1,2,3], '2')).toBe(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(countryCurrencyPairs) {
const lookup= {};
for (let pair of countryCurrencyPairs){
const country = pair[0];
const currency = pair[1];
lookup[country]= currency;

}
return lookup;
}

module.exports = createLookup;
7 changes: 5 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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", () => {
expect(createLookup([['US' , 'USD'], ['CA', 'CAD']])).toEqual({US: 'USD', CA: 'CAD'});
expect(createLookup([['GB', 'GBP'],['AU', 'AUD']])).toEqual({GB: 'GBP', AU: 'AUD'});
});
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down Expand Up @@ -33,3 +35,4 @@ It should return:
'CA': 'CAD'
}
*/

42 changes: 34 additions & 8 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
function parseQueryString(queryString) {
//function parseQueryString(queryString) {
//const queryParams = {};
//if (queryString.length === 0) {
//return queryParams;
//}
//const keyValuePairs = queryString.split("&");

//for (const pair of keyValuePairs) {

//const [key, value] = pair.split("=");
//queryParams[key] = value;
//}
//return queryParams;
//}

//module.exports = parseQueryString;

function parseQueryString(queryString){
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}

const keyValuePairs = queryString.split("&");

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

return queryParams;
}
const key = pair.slice(0, index);
const value = pair.slice(index+1);

module.exports = parseQueryString;
const decodedKey= decodeURIComponent(key);
const decodedValue = decodeURIComponent(value.replace(/\+/g, " "));

queryParams[decodedKey]=decodedValue;

}
return queryParams;
}


module.exports = parseQueryString;
12 changes: 10 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@

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

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

test("should split querystring by &", () => {
expect(parseQueryString("category=fruit&name=apple")).toEqual({category:"fruit", name: "apple"});
});

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
equation: "x=y 1",
});
});
});
15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array");
}
const result = {};
if (arr.length === 0) {
return {};
}
arr.forEach(item =>{
result[item]=(result[item] || 0) + 1;
});

return result;
}

module.exports = tally;
15 changes: 15 additions & 0 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ const tally = require("./tally.js");
// Given a function called tally
// When passed an array of items
// Then it should return an object containing the count for each unique item
test("tally on an array objects should return counts for each unique item", () => {
expect(tally(['c', 'f', 'c', 'l', 'g', 'p', 'c'])).toEqual({c:3, f:1,l:1,p:1,g:1});
});


// 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 should return 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 should return counts for each unique item",()=>{
expect(tally(['cat', 'cat', 'dog', 'mouse', 'bird', 'cat', 'mosquito'])).toEqual({cat:3, dog:1, mouse:1, bird:1, mosquito: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("hello world")).toThrow("Input must be an array");

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

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

return invertedObj;
}

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

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

// 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 simply takes an object and then returns it as an array of key-value pairs.It is needed in this program in order to turn an object into an array.
// d) Explain why the current return value is different from the target output

// I can't see the current return value, but I can see that the key is not being set correctly in the inverted object.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

module.exports= invert;
6 changes: 6 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const invert = require('./invert');

test("invert on an object, should return swapped keys and values", ()=>{
expect(invert({category: "fiction", film: "final fantasy"})).toEqual({"fiction": "category", "final fantasy": "film"});
expect(invert({x: 10, y: 20})).toEqual({"10": "x", "20": "y"});
});