-
-
Notifications
You must be signed in to change notification settings - Fork 149
London | ITP MAY | Jamal Laqdiem | Module Data Groups | Sprint-2 #588
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
f20fa19
0fbc995
10639d3
42cc34d
1342421
b7d7b0d
ed5bf12
0cd5b2d
4d3c8de
8bb21a6
f0ca7cf
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,3 +1,24 @@ | ||
function contains() {} | ||
const containObject = { | ||
first_name : 'john', | ||
surname: 'madim', | ||
date_of_birth : 1986, | ||
place_of_birth : 'newYork' | ||
} | ||
|
||
function contains(obj,propertyName) { | ||
if ( typeof obj !== 'object'|| obj === null || Array.isArray(obj)){ | ||
throw new Error ('Invalid first argument parameter, need to be an object.') | ||
} | ||
if (Object.keys(obj).length ===0){ | ||
return false | ||
} | ||
|
||
const keyObject = Object.keys(obj) | ||
const hasPropertyName = keyObject.includes(propertyName) | ||
return hasPropertyName | ||
|
||
} | ||
|
||
|
||
|
||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
function createLookup() { | ||
// implementation here | ||
const countryCurrency = [["MAR","MAD"],["UK","GBP"],["US","USD"]] | ||
|
||
|
||
function createLookup(dataCurrency) { | ||
|
||
return dataCurrency.reduce((acc,entry) => { | ||
const [countryCode,currencyCode]=entry | ||
acc[countryCode]= currencyCode | ||
return acc | ||
},{}) | ||
} | ||
|
||
console.log(createLookup(countryCurrency)) | ||
module.exports = createLookup; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
function tally() {} | ||
function tally(arr) { | ||
objectResult = {} | ||
if(arr.length ===0) | ||
return {} | ||
if( !Array.isArray(arr)) { | ||
throw new Error ('Error parameter must be an array.') | ||
} | ||
for(count of arr) { | ||
objectResult[count]= (objectResult[count] || 0) +1 ; | ||
} | ||
|
||
return objectResult | ||
} | ||
|
||
console.log(tally(['a','b','c','c'])) | ||
console.log(tally([])) | ||
console.log(tally("Hello")) | ||
module.exports = tally; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,24 +6,50 @@ | |
|
||
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
||
|
||
function invert(obj) { | ||
const invertedObj = {}; | ||
|
||
for (const [key, value] of Object.entries(obj)) { | ||
invertedObj.key = value; | ||
invertedObj[value] = key; | ||
} | ||
|
||
return invertedObj; | ||
|
||
} | ||
console.log(invert({a:1})) // output {1:a} | ||
console.log(invert({ a: 1, b: 2 })) // output {1:a,2:b} | ||
console.log(invert({})) //output {{}} | ||
console.log(invert({name:'Mike',age:33})) // output {Mike:'name', 33:age} | ||
|
||
// a) What is the current return value when invert is called with { a : 1 } | ||
|
||
// The return value is {key:1} | ||
// b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
|
||
//The return value is {key:2} | ||
// c) What is the target return value when invert is called with {a : 1, b: 2} | ||
|
||
// The target return value should be {1:a,2:b} | ||
// c) What does Object.entries return? Why is it needed in this program? | ||
|
||
// Object.entries return the keys and values of the object(obj) | ||
// d) Explain why the current return value is different from the target output | ||
|
||
// The difference happen when using the dot notation to access literal string instead of expression, it does not use the value of key variable. | ||
// e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
|
||
//test 1: | ||
const result1 = invert({a:1}); | ||
const expected1 = {1:'a'}; | ||
console.log(`Test 1: Input {a:1}, Expected ${JSON.stringify(expected1)}, Actual ${JSON.stringify(result1)}`); | ||
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. Why did you use JSON.stringify() here? What does it do and what benefit does it provide? 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. Thanks for reviewing, I used JSON.stringify() for readability and consistency, which it make it easy to read, compare and copy-paste. |
||
console.log(`Result: ${JSON.stringify(result1) === JSON.stringify(expected1) ? 'PASS' : 'FAIL'}`); | ||
//test 2: | ||
const result2 = invert({ a: 1, b: 2 }); | ||
const expected2 = {1:'a', 2:'b'}; | ||
console.log(`Test 2: Input {a:1, b:2}, Expected ${JSON.stringify(expected2)}, Actual ${JSON.stringify(result2)}`); | ||
console.log(`Result: ${JSON.stringify(result2) === JSON.stringify(expected2) ? 'PASS' : 'FAIL'}`); | ||
//test 3: | ||
const result3 = invert({}); | ||
const expected3 = {}; | ||
console.log(`Test 3: Input {}, Expected ${JSON.stringify(expected3)}, Actual ${JSON.stringify(result3)}`); | ||
console.log(`Result: ${JSON.stringify(result3) === JSON.stringify(expected3) ? 'PASS' : 'FAIL'}`); | ||
//test 4: | ||
const result4 = invert({name:'Mike',age:33}); | ||
const expected4 = {Mike:'name',33:'age'}; | ||
console.log(`Test 4: Input {name:'Mike',age:33}, Expected ${JSON.stringify(expected4)}, Actual ${JSON.stringify(result4)}`); | ||
console.log(`Result: ${JSON.stringify(result4) === JSON.stringify(expected4) ? 'PASS' : 'FAIL'}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log(`Test 1: Input {a:1}, Expected ${JSON.stringify(expected1)}, Actual ${JSON.stringify(result1)}`); |
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.
Very clever solution - well done
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.
thanks for reviewing.