Skip to content
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
3 changes: 2 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
//The console.log will log out undefined because there is no such a key as 0 in the address object

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +13,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
6 changes: 4 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
//1st prediction: var value will log out both-key and value and we need value only
//2nd prediction(as the 1st was wrong): MDN says that for...of doen't work with objects but for...in does

// 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 +13,6 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}
3 changes: 2 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// the issue here is with line 16 ${recipe}. I guess it will log out thw whole content of the recipe object, both key and value.

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -12,4 +13,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join('\n')}`);
10 changes: 7 additions & 3 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function contains() {}

module.exports = contains;
function contains(object, property) {
if (typeof object === 'object' && false === Array.isArray(object)) {
return object.hasOwnProperty(property);
}
else throw new Error("Not an object");
}
module.exports = contains;
15 changes: 14 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,33 @@ 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.todo("contains on empty object returns false");

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

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

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

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("it will throw an error parameters are invalid", () => {
expect(() => contains([])).toThrow(Error);
});

9 changes: 7 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function createLookup() {
// implementation here
function createLookup(array) {
let object = {};
for (const pair of array) {
object[pair[0]] = pair[1];
}
return object
}

module.exports = createLookup;

6 changes: 4 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.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'});
});
/*

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
if (pair.includes('=')){
const index = pair.indexOf("=");
const key = pair.slice(0, index);
const value = pair.slice(index+1);
queryParams[key] = value;
}
}

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

test("parses querystring values doesn't contain =", () => {
expect(parseQueryString("equationxy1")).toEqual({});
});

test("given a query string with no query parameters, returns an empty object", () => {
expect(parseQueryString("")).toEqual({});
});

test("given a query string with multiple key-value pairs, returns them in object form", () => {
expect(parseQueryString("sort=lowest&colour=yellow")).toEqual({sort: "lowest", colour: "yellow"});
});

13 changes: 12 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function tally() {}
function tally(array) {
let object = {};
if (Array.isArray(array)) {
for (const i of array) {
if (object.hasOwnProperty(i)) {
object[i] = object[i] + 1;
}
else object[i] = 1;
}
return object;}
else throw new Error("Invalid input");
}

module.exports = tally;
11 changes: 10 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ 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.todo("tally on an empty array returns an empty object");
test("returns an empty object when array is empty", () => {
expect(tally([])).toEqual({});
});

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

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("throw an error when input not an array", () => {
expect(() => tally('a', 'a', 'b', 'c')).toThrow(Error);
});
12 changes: 11 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@ 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 }
// invertedObj.key = value; this line creates a key literally with name "key". it returns {key : 1}

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// every time it meets the key and reassign it, so {key : 2}

// c) What is the target return value when invert is called with {a : 1, b: 2}
// to swap pair key-value with each other. should return {1 : a, 2: b}

// c) What does Object.entries return? Why is it needed in this program?
// it returns key-value pairs in arrays nested of array. we need Object.entries to convert key-value pairs to array because for..of doesn't work with objects.
//I wonder if we could solve the function by swapping key-value using [key, value] = [value, key] in some way? p.s. I'll delete this comment after review:)

// d) Explain why the current return value is different from the target output
// invertedObj.key = value; this line creates a key literally with name "key". it returns {key : 1}

// 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.js");


test(" should return an object with swapped keys and values", () => {
expect(invert({"a" : "1", "b": "2"})).toEqual({"1" : "a", "2" : "b"});
});
2 changes: 2 additions & 0 deletions Sprint-2/package-lock.json

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