Skip to content
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}`);
2 changes: 1 addition & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value in 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}`);
15 changes: 14 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function contains() {}
function contains(object, result) {
const isPlainObject =
typeof object === "object" && object !== null && !Array.isArray(object);

if (!isPlainObject) {
throw new Error("contains require an object");
}
for (const key in object) {
if (key === result) {
return true;
}
}
return false;
}

module.exports = contains;
20 changes: 18 additions & 2 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,34 @@ 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({})).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

test("contains passed an object and a property name, returns true", () => {
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("Contains passed with a non-existent property name, returns false", () => {
expect(contains({ a: 1, b: 2 }, "c")).toEqual(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("contains passed invalid input like an array, a string or a number will throw an error", () => {
expect(() => contains([5, "5", 6], "a")).toThrow(
new Error("contains require an object")
);
expect(() => contains("Ebra", "Ebra")).toThrow(
new Error("contains require an object")
);
expect(() => contains(1, 1)).toThrow(new Error("contains require an object"));
});
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(codePairs) {
const lookup={}
for(let [country,currency ] of codePairs){
lookup[country]=currency
}
return lookup
}

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");
describe('createLookup',()=>{
test("creates a country currency code lookup for multiple codes",()=>{
const arrayInput=[['US', 'USD'], ['CA', 'CAD']]
expect(createLookup(arrayInput)).toEqual({US: 'USD',CA:"CAD"})
});


})
Comment thread
Poonam-raj marked this conversation as resolved.

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

/*

Expand Down
15 changes: 13 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const keyValuePairs = queryString
.replace(/\+/g, " ")
.split("&")
.filter((pair) => pair !== "");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const indexFirstEqual = pair.indexOf("=");
let key, value;
if (indexFirstEqual === -1) {
key = decodeURIComponent(pair);
value = "";
} else {
((key = decodeURIComponent(pair.slice(0, indexFirstEqual))),
(value = decodeURIComponent(pair.slice(indexFirstEqual + 1))));
}
queryParams[key] = value;
}

Expand Down
14 changes: 7 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 @@ -40,9 +40,9 @@ 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",
});
});
// 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",
// });
// });
12 changes: 11 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function tally() {}
function tally(array) {
if (!Array.isArray(array)) {
throw new TypeError("tally requires an array");
}
const charCount = {};

for (const char of array) {
charCount[char] = (charCount[char] || 0) + 1;
}
return charCount;
}

module.exports = tally;
11 changes: 10 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ 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", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally with duplicate items returns the count for each unique item", () => {
expect(tally(["a", "b", "a", "b"])).toEqual({ a: 2, b: 2 });
});

// 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("tally with invalid input like a string, a number or no argument , throw an error", () => {
expect(() => tally("")).toThrow(new Error("tally requires an array"));
});
28 changes: 21 additions & 7 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Then it should swap the keys and values in the object

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

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

Expand All @@ -15,15 +15,29 @@ function invert(obj) {

return invertedObj;
}

console.log(invert({a:1,b:2}))*/
// 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 [key,value] pairs as two elements array and was used to return the object into enumerable array
// d) Explain why the current return value is different from the target output

// the function contain a bug as the dot notation here was overwriting the object in the loop also the return is not swapping the input
Comment thread
Poonam-raj marked this conversation as resolved.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

function invert(obj) {


const invertedObj = {};

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

return invertedObj;
}
Comment thread
Poonam-raj marked this conversation as resolved.

module.exports =invert
13 changes: 13 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const invert = require("./invert.js")

test('given an empty object , returns an empty object', () => {
expect(invert({})).toEqual({})
})

test('Given an object with a single pair swaps ', () => {
expect(invert({ a: "hello" })).toEqual({ "hello": "a" })
})

test('given an object with more than two pairs swaps the keys and values ', () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" })
})
Loading
Loading