-
-
Notifications
You must be signed in to change notification settings - Fork 279
London|25-ITP-September|Alexandru Pocovnicu|Sprint 2 #814
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
Changes from 21 commits
71fa218
927769b
a393af9
256a0bb
93dca5e
538d536
608a2f2
3a82eed
dbd5551
1769faf
8fd279f
1ff152e
2982bf2
cb3b23c
e56a2bf
3f4a115
493e3f1
577ae89
99caad7
f998538
e098bba
b1aa75d
55571b5
b8bdddf
a7d4b52
4464835
a236a49
532bb55
af31865
7a17d26
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,5 +1,5 @@ | ||
| // Predict and explain first... | ||
|
|
||
| //it will log everything in one line, we need to add \n | ||
| // 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? | ||
|
|
@@ -10,6 +10,8 @@ const recipe = { | |
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| console.log(`${recipe.title}, serves ${recipe.serves}, ingredients:`); | ||
|
|
||
| for (const ingredient of recipe.ingredients) { | ||
| console.log(ingredient); | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| function contains() {} | ||
| function contains(obj, property) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| throw new Error("Parameter is not an object literal"); | ||
| } | ||
| if (Object.keys(obj).length === 0) { | ||
|
||
| return false; | ||
| } | ||
|
|
||
| if (property in obj) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
|
||
| } | ||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,20 +16,39 @@ as the object doesn't contains a key of 'c' | |
| // Given a contains function | ||
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
| test("if the object contains the property, return 'true'", () => { | ||
| expect(contains({ a: 1 }, "a")).toEqual(true); | ||
| }); | ||
| test("if the object contains the property, return 'true'", () => { | ||
| expect(contains({ a: 1, "a,s,3": "op" }, ["a", "s", 3])).toEqual(true); | ||
| }); | ||
|
Comment on lines
+22
to
+24
Member
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. I feel like this is slightly surprising behaviour - the object doesn't really contain that array, it contains a stringification of that array. But this works, so I guess it is ok to test for... I would probably use a different name for the test, though, to make it clear how it's different from the test on line 19. |
||
|
|
||
| test("if the object doesn't contain the property return 'false'", () => { | ||
| expect(contains({ a: 1, d: 7 }, "m")).toEqual(false); | ||
| }); | ||
| test("if the object doesn't contain the property return 'false'", () => { | ||
| expect(contains({ a: 1, s: 7 }, [1, 2, "a"])).toEqual(false); | ||
| }); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains an empty object returns false", () => { | ||
| expect(contains({}, "a")).toEqual(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| //<<<both covered already>> | ||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("when instead of an object literal the parameter is an array throw error", () => { | ||
| expect(() => contains([], "a")).toThrow("Parameter is not an object literal"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,18 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrency) { | ||
| let countryCurrencyPairs = {}; | ||
| for (const pair of countryCurrency) { | ||
|
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. what happens if you test this solution with an argument which is not an object? |
||
| let country = pair[0]; | ||
| let currency = pair[1]; | ||
| countryCurrencyPairs[country] = currency; | ||
| } | ||
| return countryCurrencyPairs; | ||
| } | ||
| console.log( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["RO", "RON"], | ||
| ]) | ||
| ); | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,30 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
|
||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| if(pair===""){ | ||
|
||
| continue | ||
| } | ||
| const equalIndex=pair.indexOf("=") | ||
| if (equalIndex === -1) { | ||
| queryParams[pair]=""; | ||
| continue | ||
| } | ||
| const key=pair.substring(0,equalIndex) | ||
| const value=pair.substring(equalIndex+1) | ||
| queryParams[key]=value | ||
|
|
||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
| //console.log(parseQueryString("equationxy+1")); | ||
|
|
||
| module.exports = parseQueryString; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,17 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| const objectCount={} | ||
| if(!Array.isArray(arr)){ | ||
| throw new Error("Invalid input"); | ||
| } | ||
| if(arr.length===0){ | ||
| return {} | ||
| } | ||
| for(const element of arr){ | ||
| objectCount[element]=objectCount[element] ? objectCount[element] +1:1; | ||
| } | ||
| return objectCount | ||
|
|
||
| } | ||
|
|
||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,20 +10,26 @@ function invert(obj) { | |
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| //["a",1] | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| //[[ "a", 1], ["b", 2]] | ||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| //{"1":a,"2":b} | ||
|
Member
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. What is the type of a and b here? Are they strings/numbers/booleans/variables/something else? |
||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| //returns an array so we can access each element of it so we can swap their order | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| //it creates a new key:value pair with the key being "key",also it doesnt invert the key with the value | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| module.exports = invert; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| const invert = require("./invert.js"); | ||
| test("swap the keys and values in the object", () => { | ||
| expect(invert({ a: 1, asd: "d3e" })).toEqual({ 1: "a", d3e: "asd" }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.