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
17 changes: 17 additions & 0 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ const address = {
};

console.log(`My house number is ${address[0]}`);


This code doesnot work because address[0] is calling objects not array.
Instead of using ${address[0]}, we should use $[address.houseNumber].




const address = {
houseNumber: 42,
street: "Imaginary Road",
city: "Manchester",
country: "England",
postcode: "XYZ 123",
};

console.log(`My house number is ${address.houseNumber}`);
20 changes: 19 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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


const author = {
firstName: "Zadie",
lastName: "Smith",
Expand All @@ -11,6 +12,23 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value in author){
console.log(value);
}

because we use for...of loop in array instead of objects. To use it in objects, we can change it to for...in loop
In order to print out all the variable and the value , we should use console.log(${key}:${author[key]}).



const author = {
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};

for (const key in author) {
console.log(`${key}: ${author[key]}`);
}
15 changes: 15 additions & 0 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ const recipe = {
console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);


We should not call directly the variable instead we should use recipe.ingredients to call the value insides the label.


const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe.ingredients}`);

32 changes: 31 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
function contains() {}
const object = {
a: 1,
b: 2,
};

function contains(object, key) {
if (Array.isArray(object)) {
throw new Error();
}

if (!object) {
return false;
}

if (key in object) {
return true;
} else {
return false;
}
}

module.exports = contains;

/*
Implement a function called contains that checks an object contains a
particular property

E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'

E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/
43 changes: 42 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,55 @@ as the object doesn't contains a key of 'c'

// Acceptance criteria:

describe("contains function", () => {
test("return true when there is a variable inside", () => {
const object = {
a: 1,
b: 2,
};
const key = "c";
expect(contains(object, key)).toBe(false);
});

test("Given an empty object", () => {
const object = {};
const key = "c";
expect(contains(object, key)).toBe(false);
});

test("Given an object with properties", () => {
const object = {
a: 1,
b: 2,
};
const key = "b";
expect(contains(object, key)).toBe(true);
});

test("Given an object with non-existing properties", () => {
const object = {
a: 1,
b: 2,
};
const key = "c";
expect(contains(object.key)).toBe(false);
});

test("Given invalid parameters like an array", () => {
const object = ["apple", "orange"];
const key = "c";
expect(() => contains(object, key)).toThrow();
});
//if it is array !objects , you should throw errors//
});

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

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");

// Given an object with properties
// When passed to contains with an existing property name
Expand Down
12 changes: 10 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
if (!Array.isArray(pairs)) {
throw Error;
}
const myLookup = {};

for (let pair of pairs) {
myLookup[pair[0]] = pair[1];
}
return myLookup;
}

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

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

describe("lookup function", () => {
test("creates a country currency code lookup for multiple codes", () => {
const countryCurrentPairs = [
["US", "USD"],
["CA", "CAD"],
];
expect(createLookup(countryCurrentPairs)).toEqual({ US: "USD", CA: "CAD" });
});

test("it show a string and will throw error", () => {
expect(() => {
createLookup("Red");
}).toThrow();
});

test(" it throws empty array , it will return an empty object", () => {
expect(createLookup([])).toEqual({});
});
});
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down
52 changes: 46 additions & 6 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
function parseQueryString(queryString) {
const queryParams = {};
const result = {};

if (queryString.length === 0) {
return queryParams;
return result;
}

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
for (let pair of keyValuePairs) {
const cleanPair = pair.replace(/\+/g, " ");
// Step 3: Change all '+' signs into regular spaces
const firstEqualIndex = cleanPair.indexOf("=");

let key, value;

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

let decodedKey, decodedValue;
try {
// Try to decode normally
decodedKey = decodeURIComponent(key);
} catch (error) {
// If it's a broken code (like "100%"), just use the raw text instead of crashing!
decodedKey = key;
}

try {
decodedValue = decodeURIComponent(value);
} catch (error) {
decodedValue = value;
}

// Step 6: Put them into our boxes (Handling the Stretch Goal too!)
if (result.hasOwnProperty(decodedKey)) {
// If the box already has a secret message, make it a list or add to the list
if (!Array.isArray(result[decodedKey])) {
result[decodedKey] = [result[decodedKey]];
}
result[decodedKey].push(decodedValue);
} else {
// If the box is brand new, just put the message inside
result[decodedKey] = decodedValue;
}
}

return queryParams;
return result;
}

module.exports = parseQueryString;
27 changes: 13 additions & 14 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,45 @@
// 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({
equation: "a=b-2",
});
});

test("should ignore empty key-value pairs", () => {
expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({
key1: "value1",
key2: "value2",
});
expect(parseQueryString("")).toEqual({});
});

test("should accept empty string as key or as value", () => {
expect(parseQueryString("=value")).toEqual({ "": "value" });
expect(parseQueryString("key")).toEqual({ key: "" });
expect(parseQueryString("key=")).toEqual({ key: "" });
expect(parseQueryString("=")).toEqual({ "": "" });
});

test("should decode percent-encoded characters", () => {
expect(parseQueryString("%24half=1%2F2")).toEqual({
$half: "1/2",
});
});
//

test("should replace '+' by ' '", () => {
expect(parseQueryString("full+name=John+Doe")).toEqual({
"full name": "John Doe",
});
});

// Stretch exercise: Handling query strings that contain identical keys
test("should ignore extra or duplicate ampersands", () => {
expect(parseQueryString("key1=value1&key2=value2")).toEqual({
key1: "value1",
key2: "value2",
});
});

// 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 not crash when given malformed percent-encoding", () => {
// If decoding fails, it should just keep the original raw text safely
expect(parseQueryString("discount=100%")).toEqual({
discount: "100%",
});
});
25 changes: 24 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
function tally() {}
function tally(items) {
// 1. Check for bad inputs (like a string or number instead of an array)
// If it's bad, throw a massive red error code!
if (!Array.isArray(items)) {
throw new Error("Input must be an array!");
}

// 2. Create our empty chest to hold our item counts
const countObject = {};

// 3. Loop through every single item in the array
for (const item of items) {
if (countObject[item]) {
// If the block is already in our chest, increase the stack count by 1!
countObject[item] += 1;
} else {
// If it's a brand new block type, start the counter at 1!
countObject[item] = 1;
}
}

// 4. Hand back our beautifully organized chest!
return countObject;
}

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 @@ -14,6 +14,19 @@ const tally = require("./tally.js");
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
*/

test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

test("tally should return counts for each unique item", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});

test("tally should throw an error when passed an invalid input like a string", () => {
expect(() => {
tally("not an array");
}).toThrow();
});
// Acceptance criteria:

// Given a function called tally
Expand All @@ -23,7 +36,6 @@ 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");

// Given an array with duplicate items
// When passed to tally
Expand All @@ -32,3 +44,4 @@ test.todo("tally on an empty array returns an empty object");
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error

Loading
Loading