Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -2,7 +2,7 @@

// 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

//This isn't working because we need the key-value pairs, so we need a for..in loop
const author = {
firstName: "Zadie",
lastName: "Smith",
Expand All @@ -11,6 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (let value in author) {
console.log(author[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...

//We could use a for in loop to print all the ingredients
// 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 (let i in recipe.ingredients) {
console.log(`${recipe.ingredients[i]}`);
}
11 changes: 9 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function contains() {}

function contains(object, target) {
for (let index = 0; index < Object.keys(object).length; index++) {
const element = Object.keys(object)[index];
if (element === target) {
return true;
}
}
return false;
Comment thread
cjyuan marked this conversation as resolved.
Resolved
}
module.exports = contains;
29 changes: 27 additions & 2 deletions Sprint-2/implement/contains.test.js
Comment thread
cjyuan marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,44 @@ as the object doesn't contains a key of 'c'
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

test("returns true if the object contains the property, false otherwise", () => {
const currentOutput = contains({ a: 1, b: 2, c: 2 }, "c");
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
});
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("returns true if the object contains the property, false otherwise", () => {
const currentOutput = contains({}, "d");
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("returns true if the object contains the property, false otherwise", () => {
const currentOutput = contains({ a: 1, b: 2, c: 2 }, "b");
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("returns true if the object contains the property, false otherwise", () => {
const currentOutput = contains({ a: 1, b: 2, c: 2 }, "e");
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("returns true if the object contains the property, false otherwise", () => {
const currentOutput = contains(["a", "b", "c", "e"], "e");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});
Comment on lines 52 to +60

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test does not quite match the spec given on lines 52-54.

Please note that in JS, an array is a kind of object with its indices serve as its keys.

11 changes: 8 additions & 3 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(nestedArray) {
let lookup = {};
for (let i = 0; i < nestedArray.length; i++) {
let key = nestedArray[i][0];
let value = nestedArray[i][1];
lookup[key] = value;
}
return lookup;
}

module.exports = createLookup;
17 changes: 15 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");

/*

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

test("It should return an object where the keys are the country codes and the values are the corresponding currency codes", () => {
const currentOutput = createLookup([
["US", "USD"],
["CA", "CAD"],
]);
const targetOutput = { US: "USD", CA: "CAD" };
expect(currentOutput).toEqual(targetOutput);
});

test("It should return an empty object where the arrays are empty", () => {
const currentOutput = createLookup([[], []]);
const targetOutput = {};
expect(currentOutput).toEqual(targetOutput);
});
7 changes: 4 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const keyValuePairs = queryString.replaceAll("+", " ").split("&");

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Interesting approach!

queryParams[decodeURIComponent(key)] = decodeURIComponent(value);
}

return queryParams;
Expand Down
16 changes: 8 additions & 8 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Below are some test cases the implementation doesn't handle well.
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.

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

test("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
Expand Down Expand Up @@ -39,10 +39,10 @@ test("should replace '+' by ' '", () => {

// Stretch exercise: Handling query strings that contain identical keys

// Delete this test if you are not working on this optional case
test("should store values of a key in an array when the key has 2 or more values", () => {
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
key: ["value1", "value2", "value3"],
foo: "bar",
});
});
// //Delete this test if you are not working on this optional case
// test("should store values of a key in an array when the key has 2 or more values", () => {
// expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
// key: ["value1", "value2", "value3"],
// foo: "bar",
// });
// });
17 changes: 15 additions & 2 deletions Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function tally() {}

function tally(array) {
let tallyObject = {};
Comment thread
cjyuan marked this conversation as resolved.
Resolved
if (Array.isArray(array)) {
for (const item of array) {
if (tallyObject[item] === undefined) {
tallyObject[item] = 1;
} else {
tallyObject[item]++;
}
}
return tallyObject;
} else {
throw new Error("Invalid input");
}
Comment thread
risikatpopoola marked this conversation as resolved.
Resolved
}
module.exports = tally;
15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,25 @@ 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", () => {
const currentOutput = tally([]);
const targetOutput = {};
expect(currentOutput).toEqual(targetOutput);
});
// 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, it should return counts for each unique item", () => {
const currentOutput = tally(["a", "a", "a", "b"]);
const targetOutput = { a: 3, b: 1 };
expect(currentOutput).toEqual(targetOutput);
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error

test("tally on an array with invalid input like a string should throw an error", () => {
expect(() => tally("b").toThrow("Invalid input"));
});
26 changes: 13 additions & 13 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ 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 }

// 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}

// c) What does Object.entries return? Why is it needed in this program?

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

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// // 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?
// // It returns an array of an objects key-value pairs, to return the object key value pairs
// // d) Explain why the current return value is different from the target output
//Because we are not accessing the values using the dot notation, we need to use the square notation and swap the values around
// // e) Fix the implementation of invert (and write tests to prove it's fixed!)
module.exports = invert;
12 changes: 12 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const invert = require("./invert.js");

test("when invert is passed to an object, it should swap the keys and values in the object", () => {
const currentOutput = invert({ x: 10, y: 20 });
const targetOutput = { 10: "x", 20: "y" };
expect(currentOutput).toEqual(targetOutput);
});
test("when invert is passed to an object, it should swap the keys and values in the object", () => {
const currentOutput = invert({ a: 1, b: 2 });
const targetOutput = { 1: "a", 2: "b" };
expect(currentOutput).toEqual(targetOutput);
});
Loading