-
Notifications
You must be signed in to change notification settings - Fork 4
Completed first task #40
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
69dfab0
edd7a32
37ac66a
6c12332
6bf1074
80496f1
e26acd0
09c2476
a611c88
77542a2
083cd85
f43bf1a
d37edd2
163d43b
18cd42d
a8f930b
c8e746e
dba7944
5f11970
ca79732
50de1a5
36f88f1
68277d8
ff72c75
9fa2a69
50725b8
67236b6
c38b2f9
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,84 @@ | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| const newObj = { | ||||||||||||||||||||||||||||||||
| ...obj, | ||||||||||||||||||||||||||||||||
| age: obj.age + 1, | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| 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) { | ||||||||||||||||||||||||||||||||
| const newObj = {} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| newObj[key] = value | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return newObj | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| const objectAddName = Object.assign({}, obj) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| objectAddName.name = name | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return objectAddName | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| const objectDeleteProperty = Object.assign({}, obj) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| delete objectDeleteProperty[key] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return objectDeleteProperty | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| return new Error('Oh no, an error!') | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return val | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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,36 +90,101 @@ 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) { | ||||||||||||||||||||||||||||||||
| const myArray = [] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for (let i = 0; i < length; i++) { | ||||||||||||||||||||||||||||||||
| myArray.push(item) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return myArray | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // makeArrayOfItems should return an array containing all arguments passed to it | ||||||||||||||||||||||||||||||||
| // Tip: consider JavaScript's Rest parameters | ||||||||||||||||||||||||||||||||
| function makeArrayOfItems() {} | ||||||||||||||||||||||||||||||||
| function makeArrayOfItems(...item) { | ||||||||||||||||||||||||||||||||
| return item // Using the rest Parameter, it captures everything and stores it in an array | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| for (let i = 0; i < arr.length; i++) { | ||||||||||||||||||||||||||||||||
| if (arr[i] == item) { | ||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| for (let i = 0; i < arr.length; i++) { | ||||||||||||||||||||||||||||||||
| if (i == idx) { | ||||||||||||||||||||||||||||||||
| return arr[i] | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return new Error() | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| const tempArray = [] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for (let i = 0; i < arr.length; i++) { | ||||||||||||||||||||||||||||||||
| tempArray[i] = arr[i] | ||||||||||||||||||||||||||||||||
| if (arr[i] == arr[idx]) { | ||||||||||||||||||||||||||||||||
| tempArray[i] = item | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return tempArray | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| const tempArray = [] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for (let i = 0; i < arr.length; i++) { | ||||||||||||||||||||||||||||||||
| tempArray[i] = arr[i] | ||||||||||||||||||||||||||||||||
| if (i == idx) { | ||||||||||||||||||||||||||||||||
| tempArray.splice(i, 0, item) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return tempArray | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+150
to
+161
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 array insertion logic. The current implementation modifies the array during iteration and doesn't correctly handle insertion. Use array methods for a cleaner solution: function insertItemAtIndex(arr, item, idx) {
- const tempArray = []
-
- for (let i = 0; i < arr.length; i++) {
- tempArray[i] = arr[i]
- if (i == idx) {
- tempArray.splice(i, 0, item)
- }
- }
-
- return tempArray
+ return [...arr.slice(0, idx), item, ...arr.slice(idx)]
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 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) { | ||||||||||||||||||||||||||||||||
| const tempArray = [] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for (let i = 0; i < arr.length; i++) { | ||||||||||||||||||||||||||||||||
| tempArray[i] = arr[i] | ||||||||||||||||||||||||||||||||
| if (i == idx) { | ||||||||||||||||||||||||||||||||
| tempArray.splice(idx, 1) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return tempArray | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+165
to
+176
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 Simplify deletion logic. The current approach modifies the array during iteration. Use a cleaner solution: function deleteItemAtIndex(arr, idx) {
- const tempArray = []
-
- for (let i = 0; i < arr.length; i++) {
- tempArray[i] = arr[i]
- if (i == idx) {
- tempArray.splice(idx, 1)
- }
- }
-
- return tempArray
+ return arr.filter((_, index) => index !== idx)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // deleteItem should return an array with every instance of `item` removed | ||||||||||||||||||||||||||||||||
| function deleteItem(arr, item) {} | ||||||||||||||||||||||||||||||||
| function deleteItem(arr, item) { | ||||||||||||||||||||||||||||||||
| for (let i = 0; i < arr.length; i++) { | ||||||||||||||||||||||||||||||||
| if (arr[i] == item) { | ||||||||||||||||||||||||||||||||
| arr.splice(i, 0, item) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return arr | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+179
to
+187
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. Critical: Function mutates input and uses incorrect splice parameters. The function has two major issues:
Apply this fix: function deleteItem(arr, item) {
- for (let i = 0; i < arr.length; i++) {
- if (arr[i] == item) {
- arr.splice(i, 0, item)
- }
- }
-
- return arr
+ return arr.filter(el => el !== item)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // zipObject should return an object built from two arrays | ||||||||||||||||||||||||||||||||
| // For example, given ['foo', 'bar'] and [1, 2] it would return | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,24 +1,105 @@ | ||||||||||||||||||||||||||||||||
| function getType(thing) {} | ||||||||||||||||||||||||||||||||
| function getType(thing) { | ||||||||||||||||||||||||||||||||
| return typeof thing | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function isNumber(thing) {} | ||||||||||||||||||||||||||||||||
| function isNumber(thing) { | ||||||||||||||||||||||||||||||||
| if (typeof thing === 'number') { | ||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function toNumber(str) {} | ||||||||||||||||||||||||||||||||
| function toNumber(str) { | ||||||||||||||||||||||||||||||||
| return Number(str) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function isStringNumber(str) {} | ||||||||||||||||||||||||||||||||
| function isStringNumber(str) { | ||||||||||||||||||||||||||||||||
| if (isNaN(str)) { | ||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function add(a, b) {} | ||||||||||||||||||||||||||||||||
| function add(a, b) { | ||||||||||||||||||||||||||||||||
| return a + b | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function addStrings(a, b) {} | ||||||||||||||||||||||||||||||||
| function addStrings(a, b) { | ||||||||||||||||||||||||||||||||
| let num1 = parseInt(a) | ||||||||||||||||||||||||||||||||
| let num2 = parseInt(b) | ||||||||||||||||||||||||||||||||
| let num3 = num1 + num2 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function addStringsOrNumbers(a, b) {} | ||||||||||||||||||||||||||||||||
| return String(num3) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function addStringsOrNumbers(a, b) { | ||||||||||||||||||||||||||||||||
| let num1 = parseInt(a) | ||||||||||||||||||||||||||||||||
| let num2 = parseInt(b) | ||||||||||||||||||||||||||||||||
| let num3 = num1 + num2 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function isEmail(str) {} | ||||||||||||||||||||||||||||||||
| if (isNumber(a) && isNumber(b)) { | ||||||||||||||||||||||||||||||||
| return num3 | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| return String(num3) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function countIf(array, fn) {} | ||||||||||||||||||||||||||||||||
| function isEmail(str) { | ||||||||||||||||||||||||||||||||
| for (let i = 0; i < str.length; i++) { | ||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||
| str[i] === '@' && | ||||||||||||||||||||||||||||||||
| str[i] === '.' && | ||||||||||||||||||||||||||||||||
| str[i] === 'c' && | ||||||||||||||||||||||||||||||||
| str[i] === 'o' && | ||||||||||||||||||||||||||||||||
| str[i] === 'm' | ||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
GunnerTeh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function filterStringsWithCommas(str) {} | ||||||||||||||||||||||||||||||||
| function countIf(array, fn) { | ||||||||||||||||||||||||||||||||
| let count = 0 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function splitStringByCommas(str) {} | ||||||||||||||||||||||||||||||||
| for (let i = 0; i < array.length; i++) { | ||||||||||||||||||||||||||||||||
| if (fn(array[i]) != false) { | ||||||||||||||||||||||||||||||||
| count += 1 | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return count | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function filterStringsWithCommas(str) { | ||||||||||||||||||||||||||||||||
| let count = 0 | ||||||||||||||||||||||||||||||||
| for (let i = 0; i < str.length; i++) { | ||||||||||||||||||||||||||||||||
| if (str[i] == ',') { | ||||||||||||||||||||||||||||||||
| count += 1 | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (count > 0) { | ||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| function splitStringByCommas(str) { | ||||||||||||||||||||||||||||||||
| let index | ||||||||||||||||||||||||||||||||
| for (let i = 0; i < str.length; i++) { | ||||||||||||||||||||||||||||||||
| if (str[i] == ',') { | ||||||||||||||||||||||||||||||||
| index = str[i] | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| newStr = str.split(index) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return newStr | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+91
to
+102
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 missing variable declaration and simplify implementation. Critical issues:
Apply this diff to fix: function splitStringByCommas(str) {
- let index
- for (let i = 0; i < str.length; i++) {
- if (str[i] == ',') {
- index = str[i]
- }
- }
-
- newStr = str.split(index)
-
- return newStr
+ return str.split(',')
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| module.exports = { | ||||||||||||||||||||||||||||||||
| getType, | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| function callsFunction(func) { | ||
| let i = func() | ||
| return 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.
Fix index comparison logic.
The function incorrectly compares array values instead of indices, which could replace multiple elements with the same value.
function replaceItemAtIndex(arr, idx, item) { - const tempArray = [] - - for (let i = 0; i < arr.length; i++) { - tempArray[i] = arr[i] - if (arr[i] == arr[idx]) { - tempArray[i] = item - } - } - - return tempArray + const newArr = [...arr] + if (idx >= 0 && idx < arr.length) { + newArr[idx] = item + } + return newArr }📝 Committable suggestion
🤖 Prompt for AI Agents