-
Notifications
You must be signed in to change notification settings - Fork 3
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?
Conversation
WalkthroughImplements numerous previously stubbed JavaScript kata functions across objects/arrays, strings/numbers, and data-structures; adds new type-related utilities and function utilities; updates exports accordingly; adjusts VS Code ESLint save behavior; and marks exercises as completed in READMEs. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant ObjectsArrays as 1-objects-and-arrays/kata.js
participant DataStructures as 3-data-structures/*.js
rect rgb(235, 245, 255)
note over Caller,ObjectsArrays: Object/Array ops
Caller->>ObjectsArrays: addName(obj, name)
ObjectsArrays-->>Caller: newObj
Caller->>ObjectsArrays: findOneByProperty(arr, {key: val})
ObjectsArrays-->>Caller: firstMatch | undefined
end
rect rgb(240, 255, 240)
note over Caller,DataStructures: Search/Filter
Caller->>DataStructures: find(arr, {k:v})
DataStructures-->>Caller: match | undefined
Caller->>DataStructures: where(arr, {k:v,...})
DataStructures-->>Caller: matches[]
end
sequenceDiagram
participant Caller
participant Matrix as 3-data-structures/matrix.js
Caller->>Matrix: getMatrix(n)
Matrix-->>Caller: matrix[n][n] of 0
Caller->>Matrix: updateMatrix(matrix, [x,y], value)
Matrix-->>Caller: matrix (mutated)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 13
🧹 Nitpick comments (5)
3-JSKata/README.md (1)
85-88
: Tiny copy edit: pluralize “Objects”.Consider “Objects and arrays” for correctness and consistency.
- - [x] [Object and arrays](./1-objects-and-arrays/README.md) (2hrs) + - [x] [Objects and arrays](./1-objects-and-arrays/README.md) (2hrs)README.md (1)
21-23
: Copy edit: standardize “JavaScript Katas”.Current text has spacing and an apostrophe. Suggest the common form below.
- - [x] 3. [Java Script Kata's](/3-JSKata/README.md) (8hrs) + - [x] 3. [JavaScript Katas](/3-JSKata/README.md) (8hrs)3-JSKata/3-data-structures/positions.js (1)
1-7
: LGTM; optionally guard empty/non-array inputs.If you want safer helpers, return undefined for non-arrays or empty arrays.
-function getFirst(arr) { - return arr[0] -} +function getFirst(arr) { + return Array.isArray(arr) ? arr[0] : undefined +} -function getLast(arr) { - return arr[arr.length - 1] -} +function getLast(arr) { + return Array.isArray(arr) && arr.length ? arr[arr.length - 1] : undefined +}3-JSKata/3-data-structures/matrix.js (1)
1-8
: Validate n and simplify constructionGuard invalid n and use Array.from for clarity/perf.
-function getMatrix(n) { - let newMatrix = [] - for (let i = 0; i < n; i++) { - const row = Array(n).fill(0) - newMatrix.push(row) - } - return newMatrix -} +function getMatrix(n) { + if (!Number.isInteger(n) || n < 0) throw new TypeError('n must be a non-negative integer') + return Array.from({ length: n }, () => Array(n).fill(0)) +}3-JSKata/1-objects-and-arrays/kata.js (1)
141-149
: Prefer strict equality and for...ofAvoid coercion and use direct element iteration.
function deleteItem(arr, item) { - let newArr = [] - for (const i in arr) { - if (arr[i] != item) { - newArr.push(arr[i]) - } - } - return newArr + const newArr = [] + for (const el of arr) { + if (el !== item) newArr.push(el) + } + return newArr }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (14)
.vscode/settings.json
(1 hunks)3-JSKata/1-objects-and-arrays/kata.js
(2 hunks)3-JSKata/2-strings-and-numbers/utilities.js
(1 hunks)3-JSKata/3-data-structures/find.js
(1 hunks)3-JSKata/3-data-structures/getPropTypes.js
(1 hunks)3-JSKata/3-data-structures/getType.js
(1 hunks)3-JSKata/3-data-structures/getValue.js
(1 hunks)3-JSKata/3-data-structures/matrix.js
(1 hunks)3-JSKata/3-data-structures/positions.js
(1 hunks)3-JSKata/3-data-structures/where.js
(1 hunks)3-JSKata/4-types/functions.js
(1 hunks)3-JSKata/4-types/types.js
(1 hunks)3-JSKata/README.md
(1 hunks)README.md
(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (9)
3-JSKata/2-strings-and-numbers/utilities.js (12)
3-JSKata/3-data-structures/3-tests/getType.test.js (1)
getType
(1-1)3-JSKata/2-strings-and-numbers/2-tests/getType.test.js (1)
getType
(1-1)3-JSKata/2-strings-and-numbers/2-tests/isNumber.test.js (1)
isNumber
(1-1)3-JSKata/2-strings-and-numbers/2-tests/toNumber.test.js (1)
toNumber
(1-1)3-JSKata/2-strings-and-numbers/2-tests/isStringNumber.test.js (1)
isStringNumber
(1-1)3-JSKata/2-strings-and-numbers/2-tests/add.test.js (1)
add
(1-1)3-JSKata/2-strings-and-numbers/2-tests/addStrings.test.js (1)
addStrings
(1-1)3-JSKata/2-strings-and-numbers/2-tests/addStringsOrNumbers.test.js (1)
addStringsOrNumbers
(1-1)3-JSKata/2-strings-and-numbers/2-tests/isEmail.test.js (1)
isEmail
(1-1)3-JSKata/2-strings-and-numbers/2-tests/countIf.test.js (1)
countIf
(1-1)3-JSKata/2-strings-and-numbers/2-tests/filterStringsWithCommas.test.js (1)
filterStringsWithCommas
(1-1)3-JSKata/2-strings-and-numbers/2-tests/splitStringByCommas.test.js (2)
filterStringsWithCommas
(2-2)splitStringByCommas
(1-1)
3-JSKata/3-data-structures/getType.js (2)
3-JSKata/3-data-structures/3-tests/getType.test.js (1)
getType
(1-1)3-JSKata/2-strings-and-numbers/2-tests/getType.test.js (1)
getType
(1-1)
3-JSKata/3-data-structures/matrix.js (1)
3-JSKata/3-data-structures/3-tests/matrix.test.js (1)
matrix
(1-1)
3-JSKata/3-data-structures/getValue.js (1)
3-JSKata/3-data-structures/3-tests/getValue.test.js (1)
getValue
(1-1)
3-JSKata/3-data-structures/getPropTypes.js (1)
3-JSKata/3-data-structures/3-tests/getPropTypes.test.js (1)
getPropTypes
(1-1)
3-JSKata/3-data-structures/find.js (2)
3-JSKata/3-data-structures/3-tests/find.test.js (1)
find
(2-2)3-JSKata/3-data-structures/where.js (1)
searchKey
(3-3)
3-JSKata/4-types/functions.js (1)
3-JSKata/3-data-structures/matrix.js (1)
i
(3-3)
3-JSKata/3-data-structures/where.js (1)
3-JSKata/3-data-structures/3-tests/where.test.js (1)
where
(2-2)
3-JSKata/1-objects-and-arrays/kata.js (2)
3-JSKata/1-objects-and-arrays/1-tests/addName.test.js (2)
name
(5-5)obj
(6-8)3-JSKata/1-objects-and-arrays/1-tests/hasItem.test.js (2)
arr
(5-5)arr
(16-16)
🔇 Additional comments (4)
.vscode/settings.json (1)
8-10
: Confirm intended ESLint behavior on save."explicit" disables auto-fix on save and only runs when you trigger “Fix all”. If you intended auto-fix on save, set it to true instead.
- "source.fixAll.eslint": "explicit" + "source.fixAll.eslint": true3-JSKata/3-data-structures/getValue.js (1)
1-3
: LGTM.Simple and correct for the kata.
3-JSKata/3-data-structures/getType.js (1)
1-3
: LGTM with a note on edge cases.typeof null === 'object' and arrays return 'object'. If tests ever need special handling, consider detecting those explicitly.
3-JSKata/4-types/types.js (1)
1-34
: LGTMSimple getters match intent; no issues spotted.
function ageOneYear(obj) { | ||
console.log(obj) | ||
let newObj = { | ||
...obj, | ||
age: obj['age'] + 1, | ||
} | ||
console.log(newObj) | ||
|
||
return newObj | ||
} |
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.
-function ageOneYear(obj) {
- console.log(obj)
- let newObj = {
- ...obj,
- age: obj['age'] + 1,
- }
- console.log(newObj)
-
- return newObj
-}
+function ageOneYear(obj) {
+ return { ...obj, age: obj.age + 1 }
+}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function ageOneYear(obj) { | |
console.log(obj) | |
let newObj = { | |
...obj, | |
age: obj['age'] + 1, | |
} | |
console.log(newObj) | |
return newObj | |
} | |
function ageOneYear(obj) { | |
return { ...obj, age: obj.age + 1 } | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/1-objects-and-arrays/kata.js around lines 13 to 22, remove the two
console.log debugging lines and simplify the function to return the updated
object directly; replace the current creation of newObj and intermediate logs by
returning a new object that spreads obj and sets age to obj.age + 1 (no logging,
no unused variables).
function getItemAtIndex(arr, idx) { | ||
if (arr[idx]) { | ||
return arr[idx] | ||
} else { | ||
return Error() | ||
} | ||
} |
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.
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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function getItemAtIndex(arr, idx) { | |
if (arr[idx]) { | |
return arr[idx] | |
} else { | |
return Error() | |
} | |
} | |
function getItemAtIndex(arr, idx) { | |
if (Number.isInteger(idx) && idx >= 0 && idx < arr.length) return arr[idx] | |
return Error() | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/1-objects-and-arrays/kata.js around lines 107 to 113, the current
existence check uses arr[idx] which rejects valid falsy values (0, '', false);
replace it with an explicit bounds check (e.g. idx >= 0 && idx < arr.length) so
valid falsy entries are returned, and return the item when in-bounds otherwise
return a new Error (or appropriate error value/message).
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 | ||
} |
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.
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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
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 | |
} | |
function findAll(arr, search) { | |
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 | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/1-objects-and-arrays/kata.js around lines 182 to 192, the for-of
loop uses an undeclared variable obj causing an implicit global; declare the
loop variable (e.g., change to "for (const obj of arr)" or "for (let obj of
arr)") to avoid polluting global scope and ensure proper block scoping.
function isStringNumber(str) { | ||
return typeof str == 'string' && !isNaN(str) | ||
} |
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
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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function isStringNumber(str) { | |
return typeof str == 'string' && !isNaN(str) | |
} | |
function isStringNumber(str) { | |
return typeof str === 'string' | |
&& str.trim() !== '' | |
&& Number.isFinite(Number(str)) | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/2-strings-and-numbers/utilities.js around lines 13-15, the
isStringNumber implementation treats whitespace-only strings as numbers; change
the check to first ensure str is a string, then call str.trim() and if the
trimmed string is non-empty convert it with Number(...) and use
Number.isFinite(...) to verify it's a finite numeric value (i.e., return typeof
str === 'string' && str.trim() !== '' && Number.isFinite(Number(str.trim()))).
function isEmail(str) { | ||
console.log | ||
const emailRegex = /.+@.+\..+/ | ||
return emailRegex.test(str) | ||
} |
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 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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function isEmail(str) { | |
console.log | |
const emailRegex = /.+@.+\..+/ | |
return emailRegex.test(str) | |
} | |
function isEmail(str) { | |
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ | |
return emailRegex.test(str) | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/2-strings-and-numbers/utilities.js around lines 33 to 37, remove the
stray bare console.log call and anchor the email regex to avoid false positives;
replace the regex /.+@.+\..+/ with an anchored pattern like /^.+@.+\..+$/ (or a
tighter RFC-compliant pattern if desired) and keep the function returning
emailRegex.test(str).
function updateMatrix(matrix, coords, value) { | ||
const x = coords[0] | ||
const y = coords[1] | ||
matrix[x][y] = value | ||
return matrix | ||
} |
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
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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
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 | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/3-data-structures/matrix.js around lines 10 to 15, the updateMatrix
function writes to matrix[x][y] without validating coords or types; add input
and bounds checks: verify matrix is an array of arrays, coords is an array/tuple
of two finite integers, and value is acceptable for your domain; check 0 <= x <
matrix.length and 0 <= y < matrix[x].length (or handle ragged rows) and throw a
clear TypeError/RangeError if validations fail; only perform the assignment and
return the matrix after all checks pass.
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 | ||
} |
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 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
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
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 | |
} | |
function where(arr, searchDetails) { | |
const keys = Object.keys(searchDetails) | |
return arr.filter((obj) => keys.every((k) => obj && obj[k] === searchDetails[k])) | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/3-data-structures/where.js around lines 1-20, remove the console.log
and simplify/short-circuit the matching logic while using strict equality:
compute the searchKeys with Object.keys(searchDetails), iterate over them with a
for...of (or use Array.prototype.every) and break/short-circuit as soon as a
non-match is found, comparing values with === instead of !=; push matching
objects to newArr (or return arr.filter(...) directly) and return the result
without any console output.
function filter(arr, func) { | ||
const elements = [] | ||
for (i in arr) { | ||
if (func(arr[i])) { | ||
elements.push(arr[i]) | ||
} | ||
} | ||
return elements | ||
} |
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.
Avoid for...in on arrays and implicit globals
for...in iterates keys (and inherited) and i is undeclared; use for...of.
function filter(arr, func) {
const elements = []
- for (i in arr) {
- if (func(arr[i])) {
- elements.push(arr[i])
- }
- }
+ for (const el of arr) {
+ if (func(el)) elements.push(el)
+ }
return elements
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function filter(arr, func) { | |
const elements = [] | |
for (i in arr) { | |
if (func(arr[i])) { | |
elements.push(arr[i]) | |
} | |
} | |
return elements | |
} | |
function filter(arr, func) { | |
const elements = [] | |
for (const el of arr) { | |
if (func(el)) elements.push(el) | |
} | |
return elements | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/4-types/functions.js lines 9-17, the function uses for...in and an
undeclared i which iterates inherited keys and creates an implicit global;
replace it with a proper array iteration (e.g., for (const item of arr) { if
(func(item)) elements.push(item); }) or use arr.forEach/arr.filter, and ensure
any loop variable is declared with let/const to avoid globals.
function find(arr, func) { | ||
for (i in arr) { | ||
if (func(arr[i])) { | ||
return arr[i] | ||
} | ||
} | ||
} |
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.
Same issue in find
Switch to for...of to avoid global i and prototype keys.
function find(arr, func) {
- for (i in arr) {
- if (func(arr[i])) {
- return arr[i]
- }
- }
+ for (const el of arr) {
+ if (func(el)) return el
+ }
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function find(arr, func) { | |
for (i in arr) { | |
if (func(arr[i])) { | |
return arr[i] | |
} | |
} | |
} | |
function find(arr, func) { | |
for (const el of arr) { | |
if (func(el)) return el | |
} | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/4-types/functions.js around lines 19 to 25, the current
implementation uses "for (i in arr)" which leaks a global i and iterates
prototype keys; replace it with a for...of loop such as "for (const item of
arr)" and call func(item), returning item when true; also ensure the function
returns undefined (or null) explicitly if no element matches.
function map(arr, func) { | ||
const elements = [] | ||
for (i in arr) { | ||
elements.push(func(arr[i])) | ||
} | ||
return elements | ||
} |
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.
Same issue in map
Use for...of and local variable.
function map(arr, func) {
const elements = []
- for (i in arr) {
- elements.push(func(arr[i]))
- }
+ for (const el of arr) {
+ elements.push(func(el))
+ }
return elements
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
function map(arr, func) { | |
const elements = [] | |
for (i in arr) { | |
elements.push(func(arr[i])) | |
} | |
return elements | |
} | |
function map(arr, func) { | |
const elements = [] | |
for (const el of arr) { | |
elements.push(func(el)) | |
} | |
return elements | |
} |
🤖 Prompt for AI Agents
In 3-JSKata/4-types/functions.js around lines 27-33, the map implementation uses
a for-in loop and an undeclared loop variable which can iterate over inherited
properties and leak a global; change it to use a for...of loop with a local loop
variable (e.g., for (const item of arr)) and call func(item) when pushing into
the elements array so iteration is safe and variables are properly scoped.
Summary by CodeRabbit
New Features
Documentation
Chores