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
4 changes: 2 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

//To access object properties using their keys, not array indexes.
// 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 +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: 4 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

//we need to add Object.values(author) to returns an array containing all the values of the object.
// 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,8 @@ const author = {
alive: true,
};

for (const value of author) {
// for (const value of author) {
// console.log(value);
for (const value of Object.values(author)) {
console.log(value);
}
10 changes: 6 additions & 4 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

//The title and number of servers will display correctly. However, since the ingredients are stored in an array, I can use recipe.ingredients with a for...of loop to iterate through the array and display each ingredient on a new line.
// 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 @@ -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}
ingredients:`);
for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
7 changes: 6 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}
function contains(object,property) {
if (typeof object !== "object" || object === null || Array.isArray(object)) {
throw new Error("contains requires an object");
}

return Object.hasOwn(object, property);
}
module.exports = contains;
18 changes: 16 additions & 2 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,32 @@ as an array isn't an object
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");

test("contains on empty object returns false", () => {
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("given an object with properties returns true", () => {
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("given an object with properties contains with a non-existent property name returns false", () => {
expect(contains({c: 1, b: 2}, "a")).toBe(false);
});

// Given a value that isn't an object - an array, a string, a number,
// null, or no argument at all
// When passed to contains
// Then it should throw Error("contains requires an object")
// (careful: typeof [] and typeof null are both "object")
test("given a value that isn't an object - an array, a string, a number, null, or no argument at all", () => {
expect(() => contains([1, 2, 3], 'a')).toThrow("contains requires an object");
expect(() => contains("bcd", 'a')).toThrow("contains requires an object");
expect(() => contains(123, 'a')).toThrow("contains requires an object");
expect(() => contains(null, 'a')).toThrow("contains requires an object");
expect(() => contains()).toThrow("contains requires an object");
});
13 changes: 11 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const c_Lookup = {};

for (let i = 0; i < countryCurrencyPairs.length; i++) {
const country = countryCurrencyPairs[i][0];
const currency = countryCurrencyPairs[i][1];

c_Lookup[country] = currency;
}

return c_Lookup;
}

module.exports = createLookup;
14 changes: 13 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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 countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
["GB", "GPB"],

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fixture uses GPB for GB, and the expected object repeats that value, so this test accepts the wrong currency code. What code should this mapping use?

To "like" or "dislike" this comment, please follow this link

];

expect(createLookup(countryCurrencyPairs)).toEqual({
US: "USD",
CA: "CAD",
GB: "GPB",
});
});

/*

Expand Down
19 changes: 18 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,24 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
if (pair === "") {
continue;
}
const equalsIndex = pair.indexOf("=");
let key;
let value;

if (equalsIndex === -1) {
key = pair;
value = "";
} else {
key = pair.slice(0, equalsIndex);
value = pair.slice(equalsIndex + 1);
}

key = decodeURIComponent(key.replace(/\+/g, " "));
value = decodeURIComponent(value.replace(/\+/g, " "));

queryParams[key] = value;
}

Expand Down
32 changes: 25 additions & 7 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 @@ -37,12 +37,30 @@ test("should replace '+' by ' '", () => {
});
});

// Stretch exercise: Handling query strings that contain identical keys
test("should return an empty object for an empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

// 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",
test("should handle multiple equals signs", () => {
expect(parseQueryString("a=b=c=d")).toEqual({
a: "b=c=d",
});
});

test("should handle multiple values with empty values", () => {
expect(parseQueryString("name=&age=25")).toEqual({
name: "",
age: "25",
});
});

test("should decode spaces", () => {
expect(parseQueryString("first%20name=Abakar%20Souleyman")).toEqual({
"first name": "Abakar Souleyman",
});
});
test("should handle multiple plus signs", () => {
expect(parseQueryString("full+user+name=A+M+S")).toEqual({
"full user name": "A M S",
});
});
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("tally requires an array");
}
const list_of_items= {};

for (const item of items) {
if (list_of_items [item]) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does list_of_items[item] return on the first occurrence of an item such as "toString"?

To "like" or "dislike" this comment, please follow this link

list_of_items[item]++;
} else {
list_of_items[item] = 1;
}

}
return list_of_items;
}

module.exports = tally;
22 changes: 18 additions & 4 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,30 @@ 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 passed an array of items returns an object containing the count for each unique item", () => {
expect(tally(["a"])).toEqual({a: 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 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("given an array with duplicate items returns 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, a number, or no argument at all
// When passed to tally
// Then it should throw Error("tally requires an array")

test("throws an error for a string", () => {
expect(() => tally("abc")).toThrow("tally requires an array");

expect(() => tally(123)).toThrow("tally requires an array");

expect(() => tally()).toThrow("tally requires an array");
});

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

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

return invertedObj;
}

console.log(invert);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What output appears whenever a test or another module imports this file?

To "like" or "dislike" this comment, please follow this link

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

// -Object.entries() returns an array (list) of pairs containing the keys and values from an object.
// -It's needed so we can access both the key & the value and swap them.
// d) Explain why the current return value is different from the target output

// Because invertedObj.key = value always uses the word 'key' as the property name. Instead, we should use invertedObj[value] = key to swap the original key and value.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
module.exports = invert;
8 changes: 8 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const invert = require("./invert.js");

test("should swap keys and values", () => {
expect(invert({ a: 1, b: 2 })).toEqual({
1: "a",
2: "b"
});
});
Loading