Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}`);
13 changes: 9 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// The program throws an error because a for...of loop only works with iterable values like arrays or strings.
// The author variable is an object, which is not iterable by default, so JavaScript cannot loop through it.
// 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 +11,11 @@ const author = {
age: 40,
alive: true,
};

for (const value of author) {
console.log(value);
const values = Object.values(author);
for(const item of values)
{
console.log(item);
}



14 changes: 10 additions & 4 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

//The title and serves values print correctly because the properties are accessed directly using
// recipe.title and recipe.serves.
// 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 @@ -10,6 +11,11 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`);
for(const ingredients of recipe.ingredients)
{
console.log(`*${ingredients}`)
}

//The program prints the recipe title and number of servings.
// Then it loops through the ingredients array and logs each ingredient on a new line so they are displayed individually instead of printing the whole object.
20 changes: 19 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
function contains() {}
function contains(object, propertyName) {
if(typeof object !== 'object' || Array.isArray(object) || object=== null)
{
throw new Error("It is a invalid object")
}
const newArray = Object.keys(object);
for(let item of newArray)
{
if (item === propertyName)
{
return true
}



}
return false;
}


module.exports = contains;
32 changes: 32 additions & 0 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

test("An empty object should return false",()=>{
const object = {};
const propertyName = "a";
expect(contains(object, propertyName)).toEqual(false);
});


test("returns true when the object contains an existing property name", () => {
const object = { a: 1, b: 2, c: 3 };
const propertyName = "a";
expect(contains(object, propertyName)).toEqual(true);
});
test("returns false when the object not contains an existing property name", () => {
const object = { a: 1, b: 2, c: 3 };
const propertyName = "d";
expect(contains(object, propertyName)).toEqual(false);
});
test("Should throw an error for an invalid parameters", () => {
const object = [1,2,3,4,5,6,7];
const propertyName = "a";
expect(() => contains(object, propertyName)).toThrow();
});
test("Should throw an error for an invalid parameters", () => {
const object = null;
const propertyName = "a";
expect(() => contains(object, propertyName)).toThrow();
});
test("Should throw an error for an invalid parameters", () => {
const object = "hi";
const propertyName = "a";
expect(() => contains(object, propertyName)).toThrow();
});
// Acceptance criteria:

// Given a contains function
Expand Down
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const countryCurrencyLookup = {};

for (let item of countryCurrencyPairs) {
const [key, value] = [item[0], item[1]];
countryCurrencyLookup[key] = value;
}
return countryCurrencyLookup;
}

module.exports = createLookup;
9 changes: 9 additions & 0 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const createLookup = require("./lookup.js");

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

test("An array of arrays representing country code and currency code should return an object where the keys are country code values are currency code",()=>{
const countryCurrencyPairs = [['IND', 'INR'], ['US', 'USD'], ['UK', 'GBR'], ['CA','CAD'] ];
expect(createLookup(countryCurrencyPairs)).toEqual({
IND: "INR",
US: "USD",
UK: "GBR",
CA: "CAD",
});
})
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down
6 changes: 4 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ function parseQueryString(queryString) {
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
for (const item of keyValuePairs) {
const pair = item.split("=");
const key = pair.shift();
const value = pair.join("=");
queryParams[key] = value;
}

Expand Down
15 changes: 15 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});
test("An empty string should return an empty object",()=>{
expect(parseQueryString("")).toEqual({});
});
test("parses querystring parameters with an empty value",()=>{
expect(parseQueryString("name=")).toEqual({name :""});
});
test("handles querystring parameters that have no = sign",()=>{
expect(parseQueryString("flag")).toEqual({flag:""})
});
test("parses querystring values containing multiple = characters",()=>{
expect(parseQueryString("token=a=b=c=d")).toEqual({token:"a=b=c=d"});
});
test("handles multiple querystring parameters when one value contains =",()=>{
expect(parseQueryString("a=1&equation=x=y+1")).toEqual({a:"1", equation:"x=y+1"});
});
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}
function tally(arr) {
const object = {};
if(!Array.isArray(arr))
{
throw new Error("Invalid input: tally expects an array");
}
if(arr.length === 0)
{
return object;
}
arr.forEach(ele =>{
object[ele] =(object[ele] || 0) + 1;

});
return object;
}

module.exports = tally;
9 changes: 9 additions & 0 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ const tally = require("./tally.js");
* tally(['a', 'a', 'a']), target output: { a: 3 }
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
*/
test("throws an error when the input is not an array", ()=>{
expect(()=>tally("hi")).toThrow();
});
test("returns an empty object when the input array is empty",()=>{
expect(tally([])).toEqual({});
});
test("returns the count of each unique item in the array", ()=>{
expect(tally(['a','b','a','c','b','a'])).toEqual({a:3,b:2,c:1})
});

// Acceptance criteria:

Expand Down
11 changes: 9 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

module.exports = invert;
// 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:1, key : 2}

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

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries() returns an array of [key, value] pairs from an object. It is used here to loop through the object and access both the key and value in each iteration.

// d) Explain why the current return value is different from the target output
// The return value is different because the program uses dot notation with a variable instead of bracket notation.
// Also, the object needs to be inverted, so the key and value must be swapped when iterating through the entries.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
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("./invert");

test("returns an object with the key and value swapped when given a single key-value pair", ()=>{
expect(invert({a:1})).toEqual({'1':'a'})
});

test("returns an object with all keys and values swapped for multiple key-value pairs",()=>{

expect(invert({a:1, b:2})).toEqual({'1':'a','2':'b'})
});
12 changes: 7 additions & 5 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
<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>
<title>Quote generator app</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>hello there</h1>
<p id="quote"></p>
<p id="author"></p>
<div id="card">
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
</div>
<script src="quotes.js"></script>
</body>
</html>
17 changes: 16 additions & 1 deletion Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,19 @@ const quotes = [
},
];

// call pickFromArray with the quotes array to check you get a random quote
function quoteGenerator()
{

let randomQuote = pickFromArray(quotes);
let accessQuote = document.querySelector("#quote");
accessQuote.textContent = randomQuote.quote;
let accessAuthor = document.querySelector("#author");
accessAuthor.textContent = randomQuote.author;

}

quoteGenerator();
let newQuote = document.querySelector("#new-quote");
newQuote.addEventListener("click",quoteGenerator)


2 changes: 2 additions & 0 deletions Sprint-3/quote-generator/quotes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ describe("Quote generator", () => {
expect(authorP).toHaveTextContent("Rosa Parks");
});
});

//completed
37 changes: 36 additions & 1 deletion Sprint-3/quote-generator/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
/** Write your CSS in here **/
body{
background-color:orange;
padding: 200px;
padding-right: 300px;
text-align: center;
font-weight: bold;
font-family:'Times New Roman', Times, serif;
font-size: 20px;
}
#quote{
color:orange;
padding: 20px;
font-size: 30px;
}
#author{
color:orange;
text-align: right;
font-size: 25px;
margin-top: 20px;
}
#card{
background-color: white;
padding: 40px;
margin-top: 20px;
}
#new-quote{
background-color: orange;
color: white;
display: block;
margin-left: auto;
margin-top: 10px;
padding: 10px 20px;
border: none;
font-size: 20px;
cursor: pointer;
}
Loading