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}`));
8 changes: 3 additions & 5 deletions Sprint-2/debug/author.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not meet the purpose of the question.
The goal of the program is to output the values ​​of all properties in the object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have changed this so it only logs the values now

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

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

// using the key word "Object.velues" and assigning it to a variable passes only the values to the Object
const author = {
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};

for (const value of author) {
console.log(value);
}
let details = Object.values(author);
console.log(details);
8 changes: 6 additions & 2 deletions Sprint-2/debug/recipe.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What problems might arise if the quantity of ingredient changes later (e.g., to 100)?

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
// 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.join("\n ")}

`);
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(countryAndCurrency);
// console.log(cash);
console.log(countryAndCurrency);
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"
}
}
12 changes: 10 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
if (queryString === undefined || queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const cutOff = pair.indexOf("=");
const key = pair.slice(0, cutOff);
const value = pair.slice(cutOff + 1, pair.length);
queryParams[key] = value;
console.log(key);
console.log(value);
}

return queryParams;
}
console.log(parseQueryString("y=8&r=y"));
console.log(parseQueryString("equation=x=y+1"));
console.log(parseQueryString("="));
console.log(parseQueryString("abcdefghijk"));

module.exports = parseQueryString;
17 changes: 15 additions & 2 deletions Sprint-2/implement/querystring.test.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some edge cases need to be considered

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have made changes to the test for more edge cases, now i need to fix the code to pass the tests

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
// Below is one test case for an edge case the implementation doesn't handle well.
// Fix the implementation for this test, 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("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
equation: "x=y+1",
});
expect(parseQueryString("FIVE=5")).toEqual({
FIVE: "5",
});
expect(parseQueryString("equation=x4=y+1")).toEqual({
equation: "x4=y+1",
});
});

test("Test for a null/empty string", () => {
expect(parseQueryString("")).toEqual({});

expect(parseQueryString("=")).toEqual({});
expect(parseQueryString("abc").toBe({}));
});
24 changes: 23 additions & 1 deletion Sprint-2/implement/tally.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function tally(arr) has use console.log
why need console.log(tally(["a", "a", "b", "c"])); ???

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
function tally() {}
function tally(arr) {
check = arr.constructor === Array;
try {
if (check !== true) {
throw new Error("input requires an array");
}

let count = {};
for (let letters of arr) {
if (count[letters]) {
count[letters]++;
} else {
count[letters] = 1;
}
}

console.log(arr);
return count;
} catch (e) {
return e.message;
}
}

console.log(tally({ 6: 7 }));
module.exports = tally;
45 changes: 44 additions & 1 deletion Sprint-2/implement/tally.test.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why test expect(tally(["a", "b", "a"])).toEqual({ a: 2, b: 1 }); twice?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,59 @@ const tally = require("./tally.js");
// Given a function called tally
// When passed an array of items
// Then it should return an object containing the count for each unique item
test("tally on an empty array returns an empty object", () => {
expect(tally(["apples", "bananas", "apples", "orange", "bananas"])).toEqual({
apples: 2,
bananas: 2,
orange: 1,
});
expect(tally(["ants", "bear", "ants", "antalope"])).toEqual({
ants: 2,
bear: 1,
antalope: 1,
});
expect(tally([4, 4, 3, 2, 2, 2, 2, "cat"])).toEqual({
4: 2,
3: 1,
2: 4,
cat: 1,
});
});

// 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 it should return counts for each unique item", () => {
expect(tally(["armadillos", "boa_constrictor", "armadillos"])).toEqual({
armadillos: 2,
boa_constrictor: 1,
});
expect(tally(["ants", "bear", "ants", "antalope"])).toEqual({
ants: 2,
bear: 1,
antalope: 1,
});
expect(tally([4, 4, 3, 2, 2, 2, 2, "cat"])).toEqual({
4: 2,
3: 1,
2: 4,
cat: 1,
});
});

// Given an invalid input like a string
// When passed to tally

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last test was not finished.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finnised now

// Then it should throw an error
test("When given an input other than an array", () => {
expect(tally("feet")).toBe("input requires an array");
expect(tally(67)).toBe("input requires an array");
expect(tally(true)).toBe("input requires an array");
expect(tally({ 3: 4 })).toBe("input requires an array");
});
38 changes: 28 additions & 10 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,42 @@

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

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
module.exports = invert;
10 changes: 10 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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",
});
});
// expect(tally([])).toEqual({});
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.

15 changes: 15 additions & 0 deletions Sprint-3/quote-generator need-review/QuoteGeneratorApp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<script defer src="quotes.js"></script>
</head>
<body onload="pickNewQuoteToDisplay()">
<h1>hello there</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" class="button" id="new-quote">New quote</button>
</body>
</html>
Loading