-
-
Notifications
You must be signed in to change notification settings - Fork 153
Sheffield | 25-ITP-May | Declan Williams | Data-Groups-Sprint-2 #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ca796ca
87aa2f8
b586c5d
3d8c0e4
0c740e3
996a55e
cbcab13
53b286f
88ad32f
60d9b7c
2307da8
ac0ffda
339ddd7
b96968f
228755f
370b07c
3b15131
b7b0215
8f6fe97
6742902
c3764f5
7525314
84e8552
a11cbcd
9539ce3
a257896
79afe87
0da94ce
2a37e5e
430f315
08460b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
// Predict and explain first... | ||
// i expect the code to run but not produce the ingredients as its not calling the key pair values of ingredients array with the recipe.ingredients call | ||
// i also expect that each part will not be produced on a new line when called as there is no new line breaks called with the \n after each property has been called in the console log | ||
|
||
// 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? | ||
// to fix this you would need to change "${recipe}`" to "${recipe.ingredients}`" | ||
// to fix each ingredient being called individually you'd use the array call on "recipe.ingredients[]\n" to call individual elements of the array and put them on a new line | ||
|
||
const recipe = { | ||
title: "bruschetta", | ||
serves: 2, | ||
ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
}; | ||
|
||
console.log(`${recipe.title} serves ${recipe.serves} | ||
ingredients: | ||
${recipe}`); | ||
console.log(`${recipe.title}\n serves ${recipe.serves}\n ingredients:\n ${recipe.ingredients[0]}\n ${recipe.ingredients[1]}\n ${recipe.ingredients[2]}\n ${recipe.ingredients[3]}`); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
function contains() {} | ||
function contains(obj, key) { | ||
// check if "obj" is an object and not an array and we are working with plain objects | ||
if (typeof obj !== "object" || Array.isArray(obj)) return false; | ||
|
||
// loop over each property name in the object | ||
for (let properties in obj){ | ||
|
||
// check that if each of the properties names matches what were looking for in the key and return true if matches | ||
if (properties === key) return true; | ||
} | ||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion
For more info, you can look up |
||
|
||
return false; // if no object or key is found return false | ||
} | ||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,16 +20,27 @@ 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("when given an empty object returns false", () => { | ||
expect(contains({}, "a")).toBe(false); | ||
}); | ||
// Given an object with properties | ||
// When passed to contains with an existing property name | ||
// Then it should return true | ||
test("when given an object with existing properties returns true", () => { | ||
expect(contains({a: 1, b: 2}, "a")).toBe(true); | ||
expect(contains({a: 2, b: 4, c: 5}, "c")).toBe(true); | ||
}); | ||
|
||
// Given an object with properties | ||
// When passed to contains with a non-existent property name | ||
// Then it should return false | ||
test("when given an object with non existent properties returns false", () => { | ||
expect(contains({a: 1, b: 2,}, "c")).toBe(false); | ||
}); | ||
|
||
// Given invalid parameters like an array | ||
// When passed to contains | ||
// Then it should return false or throw an error | ||
test("when given an invalid parameters like arrays returns false", () => { | ||
expect(contains([1, 2, 3], 1)).toBe(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check if your function can pass this test? |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
function createLookup() { | ||
function createLookup(pairs) { | ||
// implementation here | ||
const results = {}; // create an empty object to be able to store results | ||
|
||
// loop through each of the pairs in the array | ||
for (const [country, currency] of pairs){ | ||
// add new key value pairs to the object "country" is the key and "currency" is the value | ||
results[country] = currency; | ||
} | ||
|
||
return results; // returns the final object that maps each key to the corresponding values | ||
} | ||
|
||
module.exports = createLookup; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,12 @@ function parseQueryString(queryString) { | |
const keyValuePairs = queryString.split("&"); | ||
|
||
for (const pair of keyValuePairs) { | ||
const [key, value] = pair.split("="); | ||
// split only the first "=" to handle cases where the value contains "=" | ||
const [key, ...rest] = pair.split("="); | ||
|
||
// rejoin the rest incase there are "=" in the value | ||
const value = rest.join("="); | ||
|
||
queryParams[key] = value; | ||
} | ||
Comment on lines
+14
to
16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please note that in real querystring, both |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
function tally() {} | ||
function tally(array) { | ||
// ensures that the input is an array. | ||
if (!Array.isArray(array)){ | ||
// throw an error if input is not an array | ||
throw new Error("expected input to be an array"); | ||
} | ||
|
||
// make an empty object to store items counted | ||
const itemCount = {}; | ||
|
||
// loop over each item in the array | ||
for (const item of array){ | ||
// if the item hasn't been counted yet treat it as 0 | ||
itemCount[item] = (itemCount[item] || 0) + 1; // then adds 1 to either start or increment the count for this item | ||
} | ||
|
||
return itemCount; | ||
} | ||
Comment on lines
+1
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the following function call returns the value you expect?
|
||
|
||
module.exports = tally; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you modify this code so that it still works if we change the number of ingredients?