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

//My prediction is that it will log undefined because we used array index to access object value instead of key name.
// 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"]}`);
9 changes: 5 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

/* In this code the problem is that the method for...of is used for array
not for object type instead we use for...in this will work through the object
and list the key and for value We use author[property] */
// 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 @@ -10,7 +12,6 @@ const author = {
age: 40,
alive: true,
};

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

/* My prediction is that on the ingredients didn't used dot notation to access the element that why it will log undefined
instead using for..of loop we can print all the ingredient in 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 @@ -11,5 +12,7 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients`);
for (const ingredient of recipe.ingredients) {
console.log(`*${ingredient}`);
}
19 changes: 18 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function contains() {}
function contains(givenObject, keyString) {
console.log(givenObject, keyString);
if (
typeof givenObject !== "object" ||
givenObject === null ||
Array.isArray(givenObject)
) {
return false;
}
for (const keyProperty in givenObject) {
console.log(keyString);
if (keyProperty === keyString) {
return true;
}
}
return false;
}
console.log(contains({ a: 2, b: 1 }, "a"));

module.exports = contains;
26 changes: 24 additions & 2 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,38 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("should return false when an empty object is passed to the function", () => {
expect(contains({})).toEqual(false);
});

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

test("the function should return true when object contains a given property", () => {
expect(contains({ a: 1, b: 2, c: 3 }, "c")).toEqual(true);
expect(contains({ name: "lee", age: 30 }, "name")).toEqual(true);
expect(contains({ place: "spain", city: "madrid", rank: 9 }, "city")).toEqual(
true
);
expect(contains({ zone: 2, zone: 3, zone: 4 }, "zone")).toEqual(true);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("the function should return false when property don't exist in the given object", () => {
expect(contains({ a: 1, b: 2, c: 3 }, "d")).toEqual(false);
expect(contains({ year: 1998, month: 10, Date: 16 }, "age")).toEqual(false);
expect(
contains({ meal: "pizza", drink: "water", table: 3 }, "starter")
).toEqual(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("the function should return false when invalid parameter like an array is passed", () => {
expect(contains([10, 23, 34, 45], "3")).toEqual(false);
expect(contains("hello", "0")).toEqual(false);
expect(contains(["green", "yellow", "rad"], "0")).toEqual(false);
expect(contains(((30, 40, 50), 30))).toEqual(false);
});
9 changes: 7 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function createLookup() {
function createLookup(countryCurrencyPairs) {
// implementation here
const currencyPairs = {};
for (const innerPair of countryCurrencyPairs) {
const [key, value] = innerPair;
currencyPairs[key] = value;
}
return currencyPairs;
}

module.exports = createLookup;
28 changes: 27 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
test("Function should create an object that have country code as key and country currency code as value", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({
US: "USD",
CA: "CAD",
});
expect(
createLookup([
["BE", "EUR"],
["BR", "BRL"],
["FI", "FIN"],
])
).toEqual({ BE: "EUR", BR: "BRL", FI: "FIN" });
expect(
createLookup([
["CV", "CPV"],
["CN", "CHN"],
["CR", "CRI"],
["DE", "DEU"],
["IN", "IND"],
])
).toEqual({ CV: "CPV", CN: "CHN", CR: "CRI", DE: "DEU", IN: "IND" });
});

/*

Expand Down
27 changes: 25 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,31 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
if (pair.length === 0) {
continue;
} else {
const equalityPosition = pair.indexOf("=");

if (equalityPosition === -1) {
queryParams[pair] = "";
} else {
const key = decodeURIComponent(pair.slice(0, equalityPosition));
const replacedKey = key.replace("+", " ");

const value = decodeURIComponent(pair.slice(equalityPosition + 1));
const replacedValue = value.replace("+", " ");

if (queryParams[replacedKey]) {
if (!Array.isArray(queryParams[replacedKey])) {
queryParams[replacedKey] = [queryParams[replacedKey]];
}

queryParams[replacedKey].push(replacedValue);
} else {
queryParams[replacedKey] = replacedValue;
}
}
}
}

return queryParams;
Expand Down
18 changes: 16 additions & 2 deletions Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
function tally() {}

function tally(myArray) {
if (!Array.isArray(myArray)) {
throw "Error";
} else if (myArray.length === 0) {
return {};
}
let tallyObject = Object.create(null);
for (const singleItems of myArray) {
if (tallyObject[singleItems] === undefined) {
tallyObject[singleItems] = 1;
} else {
tallyObject[singleItems] += 1;
}
}
return tallyObject;
}
module.exports = tally;
24 changes: 23 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,34 @@ 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("The function should return empty object when an empty array is passed to the function", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("The function should return an object where each key is a unique item from the array, and each index is how many times that item duplicate", () => {
expect(tally(["a", "a", "a", "b", "b", "b", "c", "c", "c"])).toEqual({
a: 3,
b: 3,
c: 3,
});
expect(
tally(["manchester", "manchester", "london", "london", "leeds"])
).toEqual({ manchester: 2, london: 2, leeds: 1 });
expect(tally([1, 1, 2])).toEqual({ 1: 2, 2: 1 });
expect(tally(["a"])).toEqual({ a: 1 });
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("The function should throw an error when invalid input is passed", () => {
expect(() => {
tally("hello");
}).toThrow("Error");
/*expect(tally(134)).toThrow;
expect(tally({ a: 1, b: 3, d: 2 })).toThrow;*/
});
16 changes: 16 additions & 0 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@ function invert(obj) {
}

// a) What is the current return value when invert is called with { a : 1 }
// the current return value when invert is called with { a:1 } is { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// the current return value when invert is called with { a: 1, b: 2 } is { key: 2}

// c) What is the target return value when invert is called with {a : 1, b: 2}
// the target return value when invert is called with {a : 1, b: 2} is { '1':'a','2':'b'}

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries is needed because it will gave as the access of the key and value by separating each key value pair.

// d) Explain why the current return value is different from the target output
/*the current value is different from the targeted value because the for loop already give as access for the key value pairs
but when we declare a new object the function where looking the word key in the loop because it was dot notation and taking the
word key and pairing it withe the last loop value. */

// 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;
} */
Loading