Skip to content
Open
Show file tree
Hide file tree
Changes from 13 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...

// this code will give an error because the address is not an array and we should use dot notation to access houseNumber.
// 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}`);
8 changes: 4 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

// this code will give an error because the for...of is generally used to iterate over iterable objects like arrays or strings. thus, we should use for...in loop to iterate over the properties of an 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,6 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
}
for (const value in author) {
console.log(`${value}: ${author[value]}`);
};
14 changes: 10 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...

// This code will not log out the ingredients correctly.we should access the ingredients array specifically.
// 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,12 @@ 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:
${recipe.ingredients[0]
}
${recipe.ingredients[1]}
${recipe.ingredients[2]}
${recipe.ingredients[3]}
`);
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(input, propertyName) {
if (input && typeof input === 'object' && !Array.isArray(input)) {
return input.hasOwnProperty(propertyName);
}
return false;
};

module.exports = contains;
38 changes: 35 additions & 3 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,52 @@ as the object doesn't contains a key of 'c'
// Given a contains function
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

test("given a contains function ,returns true for existing property, false otherwise", () => {
const input = {a: 1, b: 2};
const propertyName = 'a';
const currentOutput = contains(input, propertyName);
const targetOutput = true;
expect(currentOutput).toBe(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, contains returns false",()=>{
const input = {};
const propertyName = 'anyProp';
const currentOutput = contains(input,propertyName);
const targetOutput = false;
expect(currentOutput).toBe(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, returns true for existing property",()=>{
const input = {x:10,y:20,z:30};
const propertyName = 'y';
const currentOutput = contains(input,propertyName);
const targetOutput = true;
expect(currentOutput).toBe(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, returns false for non-existing property",()=>{
const input = {x:10,y:20,z:30};
const propertyName = 'a';
const currentOutput = contains(input,propertyName);
const targetOutput = false;
expect(currentOutput).toBe(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, contains returns false",()=>{
const input = [1,2,3];
const propertyName = '0';
const currentOutput = contains(input,propertyName);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
8 changes: 6 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const lookup = {};
for (const [countryCode, currencyCode] of pairs) {
lookup[countryCode] = currencyCode;
}
return lookup;
}

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

/*

Expand Down
19 changes: 16 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
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
20 changes: 17 additions & 3 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@
const parseQueryString = require("./querystring.js")

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
});
expect(parseQueryString("equation=x=y+1")).toEqual({ equation: "x=y+1" });
});

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

test("parses empty querystring", () => {
expect(parseQueryString("")).toEqual({});
});

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

test("last value wins for duplicate keys", () => {
expect(parseQueryString("a=1&a=2")).toEqual({ a: "2" });
});
15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new TypeError('Input must be an array');
}
const tallyResult = {};
for (const item of items) {
if (tallyResult[item] === undefined) {
tallyResult[item] = 1;
} else {
tallyResult[item]++;
}
}
return tallyResult;
}

module.exports = tally;
22 changes: 21 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,35 @@ const tally = require("./tally.js");
// 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", () => {
const input = ['a', 'a', 'b', 'c'];
const expectedOutput = { a: 2, b: 1, c: 1 };
expect(tally(input)).toEqual(expectedOutput);
});
// 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 input = [];
const expectedOutput = {};
expect(tally(input)).toEqual(expectedOutput);
});

// 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 correctly", () => {
const input = ['a', 'b', 'a', 'c', 'b', 'a'];
const expectedOutput = { a: 3, b: 2, c: 1 };
expect(tally(input)).toEqual(expectedOutput);
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error

test("tally throws an error for non-array input", () => {
const input = "not an array";
expect(() => tally(input)).toThrow(TypeError);
});
59 changes: 52 additions & 7 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,69 @@

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

//function invert(obj) {
// const invertedObj = {};

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

// return invertedObj;
//}

// 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 of a given object's own enumerable string-keyed property [key, value] pairs. It is needed in this program to iterate over each key-value pair in the object so that we can swap them when creating the inverted object.

// d) Explain why the current return value is different from the target output?
// The current return value is different from the target output because in the line (invertedObj.key = value;), the property name 'key' is being used literally instead of using the variable (key). This means that every iteration of the loop is overwriting the same property 'key' in the inverted object, resulting in only the last value being stored.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)

// Fixed implementation:


function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}
module.exports = invert;

// 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 }
// Test cases:

// c) What is the target return value when invert is called with {a : 1, b: 2}
const invert = require('./invert');

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

// d) Explain why the current return value is different from the target output
test('invert single key-value pair', () => {
expect(invert({ a: 1 })).toEqual({ "1": "a" });
});

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// multiple keys:

test('invert multiple key-value pairs', () => {
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" });
});

// Empty object:

test('invert empty object', () => {
expect(invert({})).toEqual({});
});

// Non-string values:

test('invert with non-string values', () => {
expect(invert({ x: 10, y: 20 })).toEqual({ "10": "x", "20": "y" });
});
19 changes: 19 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,22 @@

3. Order the results to find out which word is the most common in the input
*/

function countWords(inputString) {

const cleanedString = inputString.replace(/[.,!?]/g, '').toLowerCase();
const wordsArray = cleanedString.split(/\s+/);
const wordCount = {};

for (const word of wordsArray) {
if (wordCount[word] === undefined) {
wordCount[word] = 1;
} else {
wordCount[word]++;
}
}

return wordCount;
}
Comment on lines +30 to +45
Copy link
Contributor

Choose a reason for hiding this comment

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

Change is optional.

Here are some test cases you can try when you have time.

countWords("constructor constructor");
countWords("          Hello    World      ");


module.exports = countWords;
6 changes: 6 additions & 0 deletions Sprint-2/stretch/mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const calculateMode = require("./mode.js");
// When calculateMode is called on the array
// Then it should return the number that appears most frequently in the array

test("calculateMode is a function ,returns the most frequent number", () => {
const nums = [1, 2, 2, 3, 4];

expect(typeof calculateMode).toBe("function");
expect(calculateMode(nums)).toBe(2);
});
// Example:
// Given [2,4,1,2,3,2,1]
// When calculateMode is called on [2,4,1,2,3,2,1]
Expand Down
Loading