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

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

for (const value of author) {
//changed code at line 14
for (const value of Object.values(author)) {
console.log(value);
}

2 changes: 1 addition & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join(", ")}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change the code on line 15 so that the code meets the requirement on line 4?

Each ingredient should be logged on a new line

14 changes: 13 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function contains() {}
function contains(object, property) {
if (Object.keys(object).length === 0 ){
return false;
}
if (object.hasOwnProperty(property)){
return true;
}
else{
return false;
}

}

module.exports = contains;

23 changes: 22 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,37 @@ 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("given an empty object, it should return false", () => {
const currentOutput = contains({});
const targetOutput = false;
expect(currentOutput).toEqual(targetOutput);
});

// 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, when passed to contains with an existing property name it should return true", () => {
const currentOutput = contains({a: 1, b: 2}, 'a') ;
const targetOutput = true;
expect(currentOutput).toEqual(targetOutput);
});


// 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, when passed to contains with a non-existent property name it should return false", () => {
const currentOutput = contains({a: 1, b: 2}, 'c') ;
const targetOutput = false;
expect(currentOutput).toEqual(targetOutput);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("given ivalid parameters like an array, when passed to contains it should return false throw an error", () => {
const currentOutput = contains([1,'a'], 'c') ;
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Arrays are objects in JavaScript, and they do have property names -- just not the same ones as objects.
    Which keys do arrays have, and how does that affect how reliable your test is?
    When testing whether the function handles arrays properly, try using a key that an array might
    realistically contain
    . Otherwise, you might get a passing test even if the function isn't checking for arrays at all.

  2. What other types of value are considered invalid parameters? Why not test your function to ensure it can properly handle ALL types of value?

const targetOutput = false;
expect(currentOutput).toEqual(targetOutput);
});
5 changes: 3 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function createLookup() {
// implementation here
function createLookup(country_currency) {
let obj = Object.fromEntries(country_currency);
return obj
}

module.exports = createLookup;
9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
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 currentOutput = createLookup([['US', 'USD'], ['CA', 'CAD']]) ;
const targetOutput = {
'US': 'USD',
'CA': 'CAD'
};
expect(currentOutput).toEqual(targetOutput);
});

/*

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 @@ -3,10 +3,14 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const keyValuePairs = queryString.split("&");//method split gives us an array

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const indexForKey = pair.indexOf('=');
const key = pair.substring(0,indexForKey);
const value = pair.substring(indexForKey+1, pair.length)


queryParams[key] = value;
Copy link
Contributor

Choose a reason for hiding this comment

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

In real query string, both key and value are percent-encoded or URL encoded.
For example,

tags%5B%5D=hello%20world -> key is tags[], value is hello world

Can your function handle URL-encoded query string?

Suggestion: Look up "How to decode a URL-encoded string in JavaScript".

}

Expand Down
3 changes: 3 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});
test("must return empty object if query string is empty", () => {
expect(parseQueryString("")).toEqual({ });
});
Comment on lines +13 to +15
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you expect from the following function calls?

parseQueryString("a=b&=&c=d")
parseQueryString("a=")
parseQueryString("=b")
parseQueryString("a=b&&c=d")
parseQueryString("a&b&c")

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(array) {
if (!Array.isArray(array)){ //check if array is array
throw new Error("Invalid input");
}
if (array.length === 0 ){
return {};
}
const obj = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the following function call returns the value you expect?

  tally(["toString", "toString"]);

Suggestion: Look up an approach to create an empty object with no inherited properties.

for (let item of array){
if (obj[item]){
obj[item]+=1;
}
else{
obj[item]=1;
}
}
return obj
}

module.exports = tally;
12 changes: 11 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ 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("Given an empty array, it 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("Given an array with duplicate items, it should return counts for each unique item ", () => {
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("Given an invalid input like a string, it should throw an error", () => {
expect(() => { tally("?") }).toThrow("Invalid input");
});
16 changes: 9 additions & 7 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
for (const [key, value] of Object.entries(obj)) { //turn object into array of smaller arrays elements
invertedObj[value]= key;
}

return invertedObj;
}
console.log(invert({ a: 1, b: 2 }))

// a) What is the current return value when invert is called with { a : 1 }
// 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 }
// 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}
// 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?
// c) What does Object.entries return? Why is it needed in this program? // turn object into array of smaller arrays elements

// d) Explain why the current return value is different from the target output
// d) Explain why the current return value is different from the target output // because we have to swap key and value at line 13

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

// Given an object
// When invert is passed this object
// Then it should swap the keys and values in the object

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}

test("Given an object, then it should swap the keys and values in the object ", () => {
expect( invert({x : 10, y : 20})).toEqual({"10": "x", "20": "y"});
});
Loading