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
3 changes: 2 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ const address = {
postcode: "XYZ 123",
};

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

houseNumber: (42, console.log(`My house number is ${address.houseNumber}`));
3 changes: 2 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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
// the "author" is an object so it needs to be in square brackets to be called

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

for (const value of author) {
for (const value of [author]) {
console.log(value);
}
10 changes: 8 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
// 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?
// The recipe is an object so it needs to be in square brackets to be call each element by its key and index.

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

console.log(`${recipe.title} serves ${recipe.serves}
console.log(` ${recipe.title}
serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients[0]}
${recipe.ingredients[1]}
${recipe.ingredients[2]}
${recipe.ingredients[3]}
`);
12 changes: 11 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function contains() {}
function contains(inPut, content) {
try {
console.log(inPut.hasOwnProperty(content));
return inPut.hasOwnProperty(content);
} catch (error) {
console.error(error);
}
}
console.log(contains({ a: 1, b: 2 }, "a"));
console.log(contains({}, 9));
// console.log(contains([a, 1, b, 2], "a"));

module.exports = contains;
35 changes: 34 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,49 @@ 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("contains on empty object returns false", () => {
expect(contains({}, "c")).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("contains correct object returns true", () => {
expect(contains({ a: 4, b: 3, c: 9 }, "c")).toEqual(true);
expect(contains({ a: 4, b: 3, c: 9 }, "a")).toEqual(true);
expect(
contains(
{ ant: "in your pants", bee: "in your bonnet", cats: "pajamas" },
"bee"
)
).toEqual(true);
expect(contains({ aa: "cars", ba: "baracus", cdeez: "nope" }, "ba")).toEqual(
true
);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains incorrect object returns false", () => {
expect(contains({ a: 4, b: 3, c: 9 }, "d")).toEqual(false);
expect(contains({ a: 4, b: 3, c: 9 }, "8")).toEqual(false);
expect(
contains(
{ ant: "in your pants", bee: "in your bonnet", cats: "pajamas" },
"wasp"
)
).toEqual(false);
expect(
contains({ aa: "cars", ba: "baracus", cdeez: "nope" }, "Hanable Smith")
).toEqual(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains incorrect object type returns false", () => {
expect(contains([], "d")).toBe(false);
expect(contains("h", "d")).toBe(false);
expect(contains(9, "d")).toBe(false);
});
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(countryAndCurrency) {
const money = new Map(countryAndCurrency);
const cash = Object.fromEntries(money);
console.log(cash);
console.log(money);
return cash;
}
createLookup([
["US", "USD"],
["CA", "CAD"],
]);

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

test.todo("creates a country currency code lookup for multiple codes");
test("contains correct object returns true", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({ US: "USD", CA: "CAD" });
});

/*

Expand Down
10 changes: 10 additions & 0 deletions Sprint-2/implement/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "implement",
"description": "coureswork",
"devDependencies": {
"jest": "^30.2.0"
},
"scripts": {
"test": "jest"
}
}
3 changes: 2 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ function parseQueryString(queryString) {

return queryParams;
}

console.log(parseQueryString("y=8&r=y"));
console.log(parseQueryString("equation=x=y+1"));
module.exports = parseQueryString;
19 changes: 18 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function tally() {}
function tally(arr) {
let count = {};
for (let letters of arr) {
if (count[letters]) {
count[letters]++;
} else {
count[letters] = 1;
}
}

console.log(arr);
return count;
}

console.log(tally(["a", "a", "b", "c"]));
module.exports = tally;
// let chars = arr.reduce(
// (accsess, currentVal, i) => ({ ...arr, [i]: currentVal }),
// {}
// );
4 changes: 3 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ 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(["a", "b", "a"]).toEqual({ a: 2, b: 1 }));
});

// Given an array with duplicate items
// When passed to tally
Expand Down
39 changes: 29 additions & 10 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@

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

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

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

return invertedObj;
}
// return invertedObj;
// }
// console.log(invert({ a: 1, b: 2 }));

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

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// it only returns 2 because the loop continues and rewrights the key

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

// d) What does Object.entries return? Why is it needed in this program
// Object.entries returns the key, value pairs for the object it is given.

// e) Explain why the current return value is different from the target output
// The invertedObj.key = value; is using the literal string "key" as its key instead of the variable key

// c) What does Object.entries return? Why is it needed in this program?
// f) Fix the implementation of invert (and write tests to prove it's fixed!)

// d) Explain why the current return value is different from the target output
function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}
console.log(invert({ a: 1, b: 2 }));

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
module.exports = invert;
5 changes: 5 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const invert = require("../interpret/invert.js");

test("test for a number followed by a letter", () => {
expect(invert({ 1: "a", 2: "b", 3: "c" })).toEqual({ a: 1, b: 2, c: 3 });
});
2 changes: 2 additions & 0 deletions Sprint-2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.