-
-
Notifications
You must be signed in to change notification settings - Fork 161
NW | 25-ITP-Sep | TzeMing Ho | Sprint 2 |sprint-2-exercises #797
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
b8e5f68
bfe0b62
de5e07a
6124343
9eaf9c8
d91f6e7
5f097e4
8fa0a48
3758f52
5c349f2
cd89aa1
4a4cd94
d5a16f5
5f76350
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,7 @@ | ||
// Predict and explain first... | ||
|
||
// I guess the problem could be induced by console.log(value), as each value is a key: value pair, but we only need the values. | ||
|
||
// 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 | ||
|
||
|
@@ -11,6 +13,9 @@ const author = { | |
alive: true, | ||
}; | ||
|
||
for (const value of author) { | ||
console.log(value); | ||
} | ||
console.log(Object.values(author)); | ||
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 if we wanted to 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. If we wanted to print each property line by line, we could get the properties in an array by Object.keys(author), map each property, and print them out one by one. Object.keys(author).map((key) => console.log(key)); |
||
|
||
// After running the code, I got TypeError: author is not iterable | ||
// so, the actual problem is an object cannot be iterated in for loop. | ||
// To print out all the values in author, I attempted Object.values(author) | ||
// which return all the values in an array [ 'Zadie', 'Smith', 'writer', 40, true ] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
function contains() {} | ||
function contains(obj, targetProperty) { | ||
if (typeof obj !== "object" || Array.isArray(obj) || obj === null) { | ||
throw new Error("Input should be an object"); | ||
} | ||
return Object.keys(obj).includes(targetProperty); | ||
} | ||
|
||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
function createLookup() { | ||
// implementation here | ||
function createLookup(arr) { | ||
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. This is looking really good 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. Yeah~ |
||
if (!Array.isArray(arr)) { | ||
throw new Error("Input should be an array of arrays of code pairs"); | ||
} | ||
|
||
const validArray = arr.every( | ||
(element) => Array.isArray(element) && element.length === 2 | ||
); | ||
if (!validArray) { | ||
throw new Error("Input should be an array of arrays of code pairs"); | ||
} | ||
|
||
return Object.fromEntries(arr); | ||
} | ||
|
||
module.exports = createLookup; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
function tally() {} | ||
function tally(arr) { | ||
if (!Array.isArray(arr)) { | ||
throw new Error("Input should be an array"); | ||
} | ||
if (arr.length === 0) { | ||
return {}; | ||
} | ||
|
||
arr = arr.flat(); | ||
|
||
let result = {}; | ||
for (element of arr) { | ||
const key = | ||
typeof element === "object" ? JSON.stringify(element) : String(element); | ||
|
||
result[key] = (result[key] || 0) + 1; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
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.
Why would it not be able to access via
address[0]
?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.
address[0] can't access houseNumber because address is an object. When address[0] is read, it is looking for a key named 0 inside the address, which leads to "undefined" since the key 0 doesn't exist.