Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.
6 changes: 4 additions & 2 deletions week-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Predict and explain first...
// This code should show the number of the house from the value in the object where is printing the number
// 42, but it will be undefined because is using the index [0] and not the key "houseNumber".

// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working

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

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);
5 changes: 3 additions & 2 deletions week-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// Should show all the values for the keys of the object, but it didn't work because needs the method
// Object.values()
// 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 +12,6 @@ const author = {
alive: true,
};

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

//It should show the tittle of the const recipe = bruschetta serves 2 and the ingredients with all the
// ingredients, but it won't because it miss the reference for the key (ingredients)
// 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 @@ -12,4 +13,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients}`);
13 changes: 12 additions & 1 deletion week-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function contains() {}
function contains(obj, prop) {
for (const key of Object.keys(obj)) {
if (key === prop) {
return true;
} else {
return false;
}
}
}

console.log(contains({ a: 1, b: 2 }, "a"));
console.log(contains({ a: 1, b: 2 }, "c"));

module.exports = contains;
33 changes: 23 additions & 10 deletions week-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
function createLookup() {
function createLookup(countryCurrencyPairs) {
// implementation here
const lookup = {};
for (const pair of countryCurrencyPairs) {
const [countryCode, currencyCode] = pair;
lookup[countryCode] = currencyCode;
}
return lookup;
}

console.log(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
);

/* ======= Test suite is provided below... =====
*/

test("converts a single pair of currency codes", () => {
expect(createLookup([["GB", "GBP"]])).toEqual({
GB: "GBP",
});
expect(createLookup([["DE", "EUR"]])).toEqual({
DE: "EUR",
});
});
// test("converts a single pair of currency codes", () => {
// expect(createLookup([["GB", "GBP"]])).toEqual({
// GB: "GBP",
// });
// expect(createLookup([["DE", "EUR"]])).toEqual({
// DE: "EUR",
// });
// });

test.todo("creates a country currency code lookup for multiple codes");
// test.todo("creates a country currency code lookup for multiple codes");
11 changes: 11 additions & 0 deletions week-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function tally(array) {
const frequency = {};
for (const item of array) {
frequency[item] = (frequency[item] || 0) + 1;
}
return frequency;
}

console.log(tally(["a"]));
console.log(tally(["a", "a", "a"]));
console.log(tally(["a", "a", "b", "c"]));
8 changes: 6 additions & 2 deletions week-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ function invert(obj) {

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

return invertedObj;
}
console.log(invert({ a: 1 }));
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}

// c) What does Object.entries return? Why is it needed in this program?
// return the value of keys in the object, is important to extract the key-value pairs of an object into an array.

// d) Explain why the current return value is different from the target output
19 changes: 19 additions & 0 deletions week-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 chapter
*/

function countWords(str) {
let newString = str.replace(/[.,/#!$%^&*;:{}=\-_`?~()]/g, "");
let newString1 = newString.toLowerCase();
const words = newString1.split(/\s+/);
const wordCounts = [];

for (const word of words) {
if (word in wordCounts) {
wordCounts[word]++;
} else {
wordCounts[word] = 1;
}
}
return wordCounts;
}

console.log(countWords("you and me and me and you"));
console.log(countWords("you, And me? and me! and You!"));
1 change: 1 addition & 0 deletions week-2/stretch/till.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const till = {
"20p": 10,
};
const totalAmount = totalTill(till);
console.log(totalAmount);

// a) What is the target output when totalTill is called with the till object

Expand Down