-
Notifications
You must be signed in to change notification settings - Fork 4
Completed third task #47
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
7f7479a
f669025
87796d2
25f383c
8cfd62b
a41163d
898811f
4464a5f
dd1bced
28e3eea
36b191e
407e320
70b58bb
d956f4a
19e9083
cc71a3e
202c6a3
86c88c7
e4ee3d2
4cea066
b09e2f7
4f566f1
422bb7c
d6f2a85
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 | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,46 +4,79 @@ | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // getGreeting should return a string containing | ||||||||||||||||||||||||||||||||||||||||||||||
| // 'Hello ' and the contents of `name` | ||||||||||||||||||||||||||||||||||||||||||||||
| function getGreeting(name) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function getGreeting(name) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return 'Hello ' + name | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // ageOneYear should return a new object with an `age` property 1 greater | ||||||||||||||||||||||||||||||||||||||||||||||
| // than the `age` property of `obj` | ||||||||||||||||||||||||||||||||||||||||||||||
| function ageOneYear(obj) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function ageOneYear(obj) { | ||||||||||||||||||||||||||||||||||||||||||||||
| console.log(obj) | ||||||||||||||||||||||||||||||||||||||||||||||
| let newObj = { | ||||||||||||||||||||||||||||||||||||||||||||||
| ...obj, | ||||||||||||||||||||||||||||||||||||||||||||||
| age: obj['age'] + 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| console.log(newObj) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return newObj | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // makeObject should return an object that looks like this: | ||||||||||||||||||||||||||||||||||||||||||||||
| // (but using the arguments passed to the function) | ||||||||||||||||||||||||||||||||||||||||||||||
| // { | ||||||||||||||||||||||||||||||||||||||||||||||
| // key: value | ||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||
| function makeObject(key, value) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function makeObject(key, value) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return { [key]: value } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // getPropertyValue should return the value of the | ||||||||||||||||||||||||||||||||||||||||||||||
| // property contained in the `key` of `obj` | ||||||||||||||||||||||||||||||||||||||||||||||
| function getPropertyValue(obj, key) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function getPropertyValue(obj, key) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return obj[key] | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // addName should return a copy of `obj` with the addition of a `name` | ||||||||||||||||||||||||||||||||||||||||||||||
| // property that has the value of the `name` argument | ||||||||||||||||||||||||||||||||||||||||||||||
| // Tip: consider the object literal spread syntax | ||||||||||||||||||||||||||||||||||||||||||||||
| function addName(obj, name) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function addName(obj, name) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newObj = { | ||||||||||||||||||||||||||||||||||||||||||||||
| ...obj, | ||||||||||||||||||||||||||||||||||||||||||||||
| name: name, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return newObj | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // deleteProperty should return a new copy of `obj` without the property name | ||||||||||||||||||||||||||||||||||||||||||||||
| // that matches the `key` parameter | ||||||||||||||||||||||||||||||||||||||||||||||
| // Tip: consider JavaScript's `delete` operator | ||||||||||||||||||||||||||||||||||||||||||||||
| function deleteProperty(obj, key) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function deleteProperty(obj, key) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newObj = { ...obj } | ||||||||||||||||||||||||||||||||||||||||||||||
| delete newObj[key] | ||||||||||||||||||||||||||||||||||||||||||||||
| return newObj | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // returnErrorIfFalsy should return a JavaScript Error object with message: | ||||||||||||||||||||||||||||||||||||||||||||||
| // 'Oh no, an error!' | ||||||||||||||||||||||||||||||||||||||||||||||
| // if val evaluates to false | ||||||||||||||||||||||||||||||||||||||||||||||
| // Tip: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error | ||||||||||||||||||||||||||||||||||||||||||||||
| function returnErrorIfFalsy(val) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function returnErrorIfFalsy(val) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (val == false) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return Error('Oh no, an error!') | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // keys should return an array of the object's property names (keys) | ||||||||||||||||||||||||||||||||||||||||||||||
| // For example, given { foo: 1, bar: 2 } it would return ['foo', 'bar'] | ||||||||||||||||||||||||||||||||||||||||||||||
| function getKeys(obj) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function getKeys(obj) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return Object.keys(obj) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // getValues should return an array of the object's own values | ||||||||||||||||||||||||||||||||||||||||||||||
| // For example, given { foo: 1, bar: 2 } it would return [1, 2] | ||||||||||||||||||||||||||||||||||||||||||||||
| function getValues(obj) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function getValues(obj) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return Object.values(obj) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||
| * Arrays | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -52,57 +85,111 @@ function getValues(obj) {} | |||||||||||||||||||||||||||||||||||||||||||||
| // makeArrayOfItem should return an array that is `length` long, made up of | ||||||||||||||||||||||||||||||||||||||||||||||
| // `item`. For example, makeArrayOfItem('foo', 2) would return: | ||||||||||||||||||||||||||||||||||||||||||||||
| // ['foo', 'foo'] | ||||||||||||||||||||||||||||||||||||||||||||||
| function makeArrayOfItem(item, length) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function makeArrayOfItem(item, length) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return Array(length).fill(item) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // makeArrayOfItems should return an array containing all arguments passed to it | ||||||||||||||||||||||||||||||||||||||||||||||
| // Tip: consider JavaScript's Rest parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| function makeArrayOfItems() {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function makeArrayOfItems() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return [...arguments] | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // hasItem should return true if `item` is present in `arr` at least once, | ||||||||||||||||||||||||||||||||||||||||||||||
| // otherwise it should return false. | ||||||||||||||||||||||||||||||||||||||||||||||
| // Tip: there is an array function that makes this straightforward | ||||||||||||||||||||||||||||||||||||||||||||||
| function hasItem(arr, item) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function hasItem(arr, item) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return arr.includes(item) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // getItemAtIndex should return arr[idx] but only if that index exists: | ||||||||||||||||||||||||||||||||||||||||||||||
| // if it doesn't, return a JavaScript Error object. | ||||||||||||||||||||||||||||||||||||||||||||||
| function getItemAtIndex(arr, idx) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function getItemAtIndex(arr, idx) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (arr[idx]) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return arr[idx] | ||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| return Error() | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+107
to
+113
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. Index existence check is incorrect arr[idx] fails for falsy values (0, '', false). Check bounds instead. function getItemAtIndex(arr, idx) {
- if (arr[idx]) {
- return arr[idx]
- } else {
- return Error()
- }
+ if (Number.isInteger(idx) && idx >= 0 && idx < arr.length) return arr[idx]
+ return Error()
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // replaceItemAtIndex should return a copy of `arr` with | ||||||||||||||||||||||||||||||||||||||||||||||
| // the element at `idx` replaced with `item` | ||||||||||||||||||||||||||||||||||||||||||||||
| // Tip: consider the array literal spread syntax | ||||||||||||||||||||||||||||||||||||||||||||||
| function replaceItemAtIndex(arr, idx, item) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function replaceItemAtIndex(arr, idx, item) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newArr = [...arr] | ||||||||||||||||||||||||||||||||||||||||||||||
| newArr[idx] = item | ||||||||||||||||||||||||||||||||||||||||||||||
| return newArr | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // insertItemAtIndex should return a copy of `arr` with `item` inserted at | ||||||||||||||||||||||||||||||||||||||||||||||
| // `idx` without overwriting any array values (the array should get longer) | ||||||||||||||||||||||||||||||||||||||||||||||
| function insertItemAtIndex(arr, item, idx) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function insertItemAtIndex(arr, item, idx) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newArr = [...arr] | ||||||||||||||||||||||||||||||||||||||||||||||
| newArr.splice(idx, 0, item) | ||||||||||||||||||||||||||||||||||||||||||||||
| return newArr | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // deleteItemAtIndex should return a copy of `arr` without | ||||||||||||||||||||||||||||||||||||||||||||||
| // the element at `idx` (the array should get shorter). | ||||||||||||||||||||||||||||||||||||||||||||||
| function deleteItemAtIndex(arr, idx) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function deleteItemAtIndex(arr, idx) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newArr = [...arr] | ||||||||||||||||||||||||||||||||||||||||||||||
| newArr.splice(idx, 1) | ||||||||||||||||||||||||||||||||||||||||||||||
| return newArr | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // deleteItem should return an array with every instance of `item` removed | ||||||||||||||||||||||||||||||||||||||||||||||
| function deleteItem(arr, item) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function deleteItem(arr, item) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newArr = [] | ||||||||||||||||||||||||||||||||||||||||||||||
| for (const i in arr) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (arr[i] != item) { | ||||||||||||||||||||||||||||||||||||||||||||||
| newArr.push(arr[i]) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return newArr | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // zipObject should return an object built from two arrays | ||||||||||||||||||||||||||||||||||||||||||||||
| // For example, given ['foo', 'bar'] and [1, 2] it would return | ||||||||||||||||||||||||||||||||||||||||||||||
| // { foo: 1, bar: 2 } | ||||||||||||||||||||||||||||||||||||||||||||||
| function zipObject(keys, values) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function zipObject(keys, values) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newObj = {} | ||||||||||||||||||||||||||||||||||||||||||||||
| for (const i in keys) { | ||||||||||||||||||||||||||||||||||||||||||||||
| newObj[keys[i]] = values[i] | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return newObj | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // unzipObject should return an array of arrays, each one a pair of keys and values | ||||||||||||||||||||||||||||||||||||||||||||||
| // For example, given {foo: 1, bar: 2} it would return | ||||||||||||||||||||||||||||||||||||||||||||||
| // [['foo', 1], ['bar', 2]] | ||||||||||||||||||||||||||||||||||||||||||||||
| function unzipObject(obj) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function unzipObject(obj) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return Object.entries(obj) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // findOneByProperty should return an object from `arr` that has the | ||||||||||||||||||||||||||||||||||||||||||||||
| // property AND value of `search`. For example, given: | ||||||||||||||||||||||||||||||||||||||||||||||
| // [{a: 1}, {b: 2, c: 3}] and {b: 2} | ||||||||||||||||||||||||||||||||||||||||||||||
| // it will return: | ||||||||||||||||||||||||||||||||||||||||||||||
| // {b: 2, c: 3} | ||||||||||||||||||||||||||||||||||||||||||||||
| function findOneByProperty(arr, search) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function findOneByProperty(arr, search) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let key = Object.keys(search)[0] | ||||||||||||||||||||||||||||||||||||||||||||||
| let value = search[key] | ||||||||||||||||||||||||||||||||||||||||||||||
| return arr.find((obj) => obj[key] == value) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // findAll should return an array containing all objects in `arr` that | ||||||||||||||||||||||||||||||||||||||||||||||
| // have the property and value of `search` | ||||||||||||||||||||||||||||||||||||||||||||||
| function findAll(arr, search) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function findAll(arr, search) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newArr = [] | ||||||||||||||||||||||||||||||||||||||||||||||
| let key = Object.keys(search)[0] | ||||||||||||||||||||||||||||||||||||||||||||||
| let value = search[key] | ||||||||||||||||||||||||||||||||||||||||||||||
| for (obj of arr) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (obj[key] == value) { | ||||||||||||||||||||||||||||||||||||||||||||||
| newArr.push(obj) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return newArr | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+182
to
+192
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. Implicit global in for-of loop obj is undeclared; declare it. function findAll(arr, search) {
- let newArr = []
- let key = Object.keys(search)[0]
- let value = search[key]
- for (obj of arr) {
+ let newArr = []
+ let key = Object.keys(search)[0]
+ let value = search[key]
+ for (const obj of arr) {
if (obj[key] == value) {
newArr.push(obj)
}
}
return newArr
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| module.exports = { | ||||||||||||||||||||||||||||||||||||||||||||||
| addName, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,24 +1,58 @@ | ||||||||||||||||||||||||||||||||||
| function getType(thing) {} | ||||||||||||||||||||||||||||||||||
| function getType(thing) { | ||||||||||||||||||||||||||||||||||
| return typeof thing | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function isNumber(thing) {} | ||||||||||||||||||||||||||||||||||
| function isNumber(thing) { | ||||||||||||||||||||||||||||||||||
| return typeof thing == 'number' | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function toNumber(str) {} | ||||||||||||||||||||||||||||||||||
| function toNumber(str) { | ||||||||||||||||||||||||||||||||||
| return Number(str) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function isStringNumber(str) {} | ||||||||||||||||||||||||||||||||||
| function isStringNumber(str) { | ||||||||||||||||||||||||||||||||||
| return typeof str == 'string' && !isNaN(str) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+15
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. 🛠️ Refactor suggestion Tighten numeric string check Current logic treats whitespace-only strings as numbers. Use trim + Number.isFinite. -function isStringNumber(str) {
- return typeof str == 'string' && !isNaN(str)
-}
+function isStringNumber(str) {
+ return typeof str === 'string' && str.trim() !== '' && Number.isFinite(Number(str))
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function add(a, b) {} | ||||||||||||||||||||||||||||||||||
| function add(a, b) { | ||||||||||||||||||||||||||||||||||
| return a + b | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function addStrings(a, b) {} | ||||||||||||||||||||||||||||||||||
| function addStrings(a, b) { | ||||||||||||||||||||||||||||||||||
| return String(Number(a) + Number(b)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function addStringsOrNumbers(a, b) {} | ||||||||||||||||||||||||||||||||||
| function addStringsOrNumbers(a, b) { | ||||||||||||||||||||||||||||||||||
| if (typeof a == 'number' && typeof b == 'number') { | ||||||||||||||||||||||||||||||||||
| return a + b | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| return String(Number(a) + Number(b)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function isEmail(str) {} | ||||||||||||||||||||||||||||||||||
| function isEmail(str) { | ||||||||||||||||||||||||||||||||||
| console.log | ||||||||||||||||||||||||||||||||||
| const emailRegex = /.+@.+\..+/ | ||||||||||||||||||||||||||||||||||
| return emailRegex.test(str) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+37
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. 🛠️ Refactor suggestion Remove stray console.log and anchor regex The bare console.log is a no-op and may fail linting. Anchor the regex to reduce false positives. -function isEmail(str) {
- console.log
- const emailRegex = /.+@.+\..+/
- return emailRegex.test(str)
-}
+function isEmail(str) {
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
+ return emailRegex.test(str)
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function countIf(array, fn) {} | ||||||||||||||||||||||||||||||||||
| function countIf(array, fn) { | ||||||||||||||||||||||||||||||||||
| count = 0 | ||||||||||||||||||||||||||||||||||
| for (const item of array) { | ||||||||||||||||||||||||||||||||||
| if (fn(item)) { | ||||||||||||||||||||||||||||||||||
| count++ | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return count | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+47
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. Fix implicit global variable count is implicitly global; declare it locally. -function countIf(array, fn) {
- count = 0
+function countIf(array, fn) {
+ let count = 0
for (const item of array) {
- if (fn(item)) {
- count++
- }
+ if (fn(item)) count++
}
return count
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function filterStringsWithCommas(str) {} | ||||||||||||||||||||||||||||||||||
| function filterStringsWithCommas(str) { | ||||||||||||||||||||||||||||||||||
| return str.includes(',') | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function splitStringByCommas(str) {} | ||||||||||||||||||||||||||||||||||
| function splitStringByCommas(str) { | ||||||||||||||||||||||||||||||||||
| return str.split(',') | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| module.exports = { | ||||||||||||||||||||||||||||||||||
| getType, | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,6 @@ | ||||||||||||||||||||||||
| function find(arr, searchDetails) {} | ||||||||||||||||||||||||
| function find(arr, searchDetails) { | ||||||||||||||||||||||||
| searchKey = Object.keys(searchDetails)[0] | ||||||||||||||||||||||||
| return arr.find((obj) => obj[searchKey] == searchDetails[searchKey]) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+1
to
+4
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. 🛠️ Refactor suggestion Fix implicit global and tighten comparisons; add basic input checks. searchKey is assigned without let/const (creates a global). Also consider strict equality and guarding empty/invalid search details. function find(arr, searchDetails) {
- searchKey = Object.keys(searchDetails)[0]
- return arr.find((obj) => obj[searchKey] == searchDetails[searchKey])
+ if (!Array.isArray(arr) || !searchDetails || typeof searchDetails !== 'object') return undefined
+ const keys = Object.keys(searchDetails)
+ if (keys.length === 0) return undefined
+ const searchKey = keys[0]
+ return arr.find((obj) => obj && obj[searchKey] === searchDetails[searchKey])
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| module.exports = find | ||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,9 @@ | ||||||||||||||||||||||||
| function getPropTypes(obj) {} | ||||||||||||||||||||||||
| function getPropTypes(obj) { | ||||||||||||||||||||||||
| let propTypes = [] | ||||||||||||||||||||||||
| for (const i in obj) { | ||||||||||||||||||||||||
| propTypes.push(typeof obj[i]) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return propTypes | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+1
to
+7
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. 🛠️ Refactor suggestion Avoid inherited props in results for...in includes enumerable prototype props; use own keys and handle non-objects. -function getPropTypes(obj) {
- let propTypes = []
- for (const i in obj) {
- propTypes.push(typeof obj[i])
- }
- return propTypes
-}
+function getPropTypes(obj) {
+ if (obj === null || typeof obj !== 'object') return []
+ return Object.keys(obj).map((k) => typeof obj[k])
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| module.exports = getPropTypes | ||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| function getType(thing) {} | ||
| function getType(thing) { | ||
| return typeof thing | ||
| } | ||
|
|
||
| module.exports = getType |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| function getValue(obj, key) {} | ||
| function getValue(obj, key) { | ||
| return obj[key] | ||
| } | ||
|
|
||
| module.exports = getValue |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,18 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| function getMatrix(n) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function getMatrix(n) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let newMatrix = [] | ||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = 0; i < n; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||
| const row = Array(n).fill(0) | ||||||||||||||||||||||||||||||||||||||||||||||
| newMatrix.push(row) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return newMatrix | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| function updateMatrix(matrix, coords, value) {} | ||||||||||||||||||||||||||||||||||||||||||||||
| function updateMatrix(matrix, coords, value) { | ||||||||||||||||||||||||||||||||||||||||||||||
| const x = coords[0] | ||||||||||||||||||||||||||||||||||||||||||||||
| const y = coords[1] | ||||||||||||||||||||||||||||||||||||||||||||||
| matrix[x][y] = value | ||||||||||||||||||||||||||||||||||||||||||||||
| return matrix | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+15
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. 🛠️ Refactor suggestion Bounds/input checks for coords Access without validation can throw or corrupt state. Add type/bounds checks. -function updateMatrix(matrix, coords, value) {
- const x = coords[0]
- const y = coords[1]
- matrix[x][y] = value
- return matrix
-}
+function updateMatrix(matrix, coords, value) {
+ const [x, y] = coords
+ if (!Array.isArray(matrix) || !Array.isArray(matrix[x])) throw new TypeError('matrix must be a 2D array')
+ if (!Number.isInteger(x) || !Number.isInteger(y) || x < 0 || y < 0 || x >= matrix.length || y >= matrix[x].length) {
+ throw new RangeError('coords out of bounds')
+ }
+ matrix[x][y] = value
+ return matrix
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| module.exports = { | ||||||||||||||||||||||||||||||||||||||||||||||
| getMatrix, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,22 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| function where(arr, searchDetails) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| function where(arr, searchDetails) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| let newArr = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const searchKey = Object.keys(searchDetails) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const obj of arr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| let match = true | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const i in searchKey) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (obj[searchKey[i]] != searchDetails[searchKey[i]]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| match = false | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if (match) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| newArr.push(obj) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log(newArr) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return newArr | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+20
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. 🛠️ Refactor suggestion Remove console.log and simplify matching; prefer strict equality. Unconditional logging is noisy. The loop can be simplified and short-circuited; use === to avoid coercion. function where(arr, searchDetails) {
- let newArr = []
- const searchKey = Object.keys(searchDetails)
-
- for (const obj of arr) {
- let match = true
-
- for (const i in searchKey) {
- if (obj[searchKey[i]] != searchDetails[searchKey[i]]) {
- match = false
- }
- }
-
- if (match) {
- newArr.push(obj)
- }
- }
- console.log(newArr)
- return newArr
+ const keys = Object.keys(searchDetails)
+ return arr.filter((obj) => keys.every((k) => obj && obj[k] === searchDetails[k]))
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| module.exports = where | ||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🛠️ Refactor suggestion
Remove debug logging and streamline
console.log calls are noisy in library code. Simplify return.
📝 Committable suggestion
🤖 Prompt for AI Agents