Skip to content
Closed
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}`);
4 changes: 2 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of of Object.values(author)) {
Copy link

Choose a reason for hiding this comment

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

This works, however please note there are more cleaner ways to achieve this. Knowing those will definitely help in the future.

console.log(value);
}
}
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 ingredient of recipe.ingredients) {
console.log(ingredient);
}
21 changes: 19 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function contains() {}
function contains(obj, prop) {
Copy link

Choose a reason for hiding this comment

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

This fulfils the requirements, now think of the below:

  • What will happen if we inverse the if condition check.
  • What does hasOwnProperty method return, do we need explicit return statement

This will help make this more better and less cluttered!

if (!obj || typeof obj !== "object" || Array.isArray(obj)){

module.exports = contains;
return false;

}
}

if (obj.hasOwnProperty(prop)){

return true;

}
else{

return false;
}


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.todo("contains on empty object returns false");
test("contains on empty object returns false", () =>{

const result = contains({}, "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("returns true when the property is there", () =>{

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("returns false when the property isn't there", ()=>{

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("invalid stuff like arrays or numbers should just return false", () =>{

expect(contains([], "a")).toBe(false);
expect(contains(null, "x")).toBe(false);
expect(contains(123, "nope")).tobe(false);

});
14 changes: 13 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
function createLookup() {
function createLookup(pairs) {
// implementation here

const lookup = {};

for (let i = 0; i < pairs.length; i++){

const country = pairs[i][0];
Copy link

Choose a reason for hiding this comment

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

There is cleaner way de-structuring of accessing the values, if we expect the array to have fixed number of items,

const country = pairs[i][1];
Copy link

Choose a reason for hiding this comment

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

Maybe a typo here, country is a constant, can it be redeclared?

lookup[country] = currency;
Copy link

Choose a reason for hiding this comment

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

Does currency exist in the global and local scope?


}

return lookup;
}

module.exports = createLookup;
24 changes: 23 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
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", () =>{
const countryCurrencyPairs =[

['US', 'USD'],
['CA', 'CAD'],
['JP', 'JPY']

];

const expected ={

'US': 'USD',
'CA': 'CAD',
'JP': 'JPY'

};

const result = createLookup(countryCurrencyPairs);

expect(result).toEqual(expected);

});

/*

Expand Down
22 changes: 18 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ function parseQueryString(queryString) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
for (let i = 0; i < keyValuePairs.length; i++){
const pair = keyValuePairs[i];
const indexOfEquals = pair.indexOf("=");
if(indexOfEquals === -1){
Copy link

Choose a reason for hiding this comment

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

Should we handle key with no value?

}
else{
const key = pair.slice(0, indexOfEquals);
const value = pair.slice(indexOfEquals + 1);
queryParams[key] = value;
}
}



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

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

return queryParams;
}


module.exports = parseQueryString;
32 changes: 32 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,35 @@ test("parses querystring values containing =", () => {
"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 empty querystring",() =>{

expect(parseQueryString("")).toEqual({});
});


test("handles key with no value",() =>{

expect(parseQueryString("foo")).toEqual({ foo:""});
});


test("handles mix of empty and non-empty values",() =>{

expect(parseQueryString("foo=&bar=42")).toEqual({

foo: "",
bar: "42",

});
});
27 changes: 26 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
function tally() {}
function tally(array) {

if (!Array.isArray(arr)) {
Copy link

Choose a reason for hiding this comment

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

Maybe a typo, does arr exist?

throw new Error("tally expects an array!");
}

const counts = {};

if (arr.length === 0){

return counts;
}
for (let i = 0; i < arr.length; i++){

const item = arr[i];

if (counts[item]) {

counts[item]= counts[item] + 1;
}
else {
counts[item]= 1;
}
Comment on lines +17 to +23
Copy link

Choose a reason for hiding this comment

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

What if we start at 0 when the key is undefined and then increment by 1 if exists. Will shorten the code for sure.

}
return counts;
}

module.exports = tally;
18 changes: 18 additions & 0 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,28 @@ const tally = require("./tally.js");
// 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 counts items correctly",() =>{

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("tally throws error on non-array input",() =>{

expect(() =>tally("not an array")).toThrow();
});
14 changes: 14 additions & 0 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ function invert(obj) {
}

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

// b) What is the current return value when invert is called with { a: 1, b: 2 }
{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?
[["a", 1], ["b", 2]]

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

Choose a reason for hiding this comment

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

This is asking why does the current invert function returns {key: 2} when { a: 1, b: 2 } is passed as argument. Shouldn't the function return {"1": "a", "2": "b"}?

Object.entries gives you: the key and the value,So we can swap them easily.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
function invert(obj) {
const invertedObj = {};

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

Choose a reason for hiding this comment

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

This will create key key in the object invertedObj, is that our goal (make key to value and value to key).

}

return invertedObj;
}
Loading