Skip to content
Open
Show file tree
Hide file tree
Changes from 15 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: 4 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Predict and explain first...
//Prediction: The will be an error at ${address[0]}.
//Explanation: calling an object using the index as in array. Instead we should use the dot method(.address) or the brackets["address"].


// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +15,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...
// Prediction: Going to shows an error in the for loop.
// Explanation: we should have used the for .. in method (The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.)

// 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(`${value}: ${author[value]}`);
}
7 changes: 6 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// Prediction: An error in logging out the ingredients.
// Explanation: Because of the way to get the value or an element from an array should used the dot notation for example and reach each element using its index.

// 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 +14,7 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients[0]}
${recipe.ingredients[1]}
${recipe.ingredients[2]}
${recipe.ingredients[3]}`);
8 changes: 7 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(input, propertyName) {
if (input && typeof input === 'object' && !Array.isArray(input)) {
return input.hasOwnProperty(propertyName);
}
return false;

}

module.exports = contains;
46 changes: 45 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,63 @@ 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("given an object contains a property name, returns true", function (){
const input = {a: 1, b: 2};
const propertyName = 'a';
const currentOutput = contains(input, propertyName);
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("given an empty object, returns false", function (){
const input = {};
const propertyName = 'a';
const currentOutput = contains(input, propertyName);
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 contains a property name, returns true", function (){
const input = {a: 1, b: 2};
const propertyName = 'a';
const currentOutput = contains(input, propertyName);
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 does not contain a property name, returns false", function (){
const input = {a: 1, b: 2};
const propertyName = 'c';
const currentOutput = contains(input, propertyName);
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 invalid parameters like an array, returns false", function (){
const input = [1, 2, 3];
const propertyName = 'a';
const currentOutput = contains(input, propertyName);
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});
12 changes: 9 additions & 3 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const lookup = {};

for (const [country, currency] of pairs) {
lookup[country] = currency;
}

return lookup;
}

module.exports = createLookup;
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,20 @@
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 input = [
["US", "USD"],
["CA", "CAD"],
["GB", "GBP"],
];

const result = createLookup(input);

expect(result).toEqual({
US: "USD",
CA: "CAD",
GB: "GBP",
});
});
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down
25 changes: 23 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {

if (!queryString) {
return queryParams;
}

if (queryString.startsWith("?")) {
queryString = queryString.slice(1);
}

const keyValuePairs = queryString.split("&");

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

const firstEq = pair.indexOf("=");

let key;
let value;

if (firstEq === -1) {

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

queryParams[key] = value;
}

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

test("parses multiple key=value pairs", () => {
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
});

test("handles empty value (a=)", () => {
expect(parseQueryString("a=")).toEqual({ a: "" });
});

test("handles key with no '=' (a)", () => {
expect(parseQueryString("a")).toEqual({ a: "" });
});

test("last value wins for repeated keys", () => {
expect(parseQueryString("a=1&a=2")).toEqual({ a: "2" });
});

test("supports leading question mark", () => {
expect(parseQueryString("?a=1&b=2")).toEqual({ a: "1", b: "2" });
});

test("ignores empty pairs from trailing or consecutive &", () => {
expect(parseQueryString("a=1&&b=2&")).toEqual({ a: "1", b: "2" });
});

test("handles empty key (=value) producing empty-string key", () => {
expect(parseQueryString("=value")).toEqual({ "": "value" });
});
18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("tally expects an array");
}

const counts = {};

for (const item of items) {
if (counts[item] === undefined) {
counts[item] = 1;
} else {
counts[item] += 1;
}
}

return counts;
}

module.exports = tally;
23 changes: 22 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,37 @@ 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 returns counts for each unique item", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 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("tally counts each unique item", () => {
expect(tally(["a", "a", "b", "c", "a"])).toEqual({
a: 3,
b: 1,
c: 1,
});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws an error when given a non-array", () => {
expect(() => tally("not an array")).toThrow();
expect(() => tally(123)).toThrow();
expect(() => tally({})).toThrow();
});
48 changes: 43 additions & 5 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,52 @@ function invert(obj) {
return invertedObj;
}

// 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? >> Object.entries returns an array of key-value paris.

// 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 invertedObj.key = value creates a property called (key) instead of using hte variable key.

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

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

return invertedObj;
}

module.exports = invert;

// Tests:
const invert = require("./invert.js");

// Single key
test("invert works for one key-value pair", () => {
expect(invert({ a: 1 })).toEqual({ "1": "a" });
});

// Multiple keys
test("invert swaps keys and values for multiple pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({
"1": "a",
"2": "b",
});
});

// Empty object
test("invert of an empty object returns empty", () => {
expect(invert({})).toEqual({});
});

// Values become string keys
test("invert always returns string keys", () => {
expect(invert({ x: 10 })).toEqual({ "10": "x" });
});
Loading