Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.codeActionsOnSave": { "source.fixAll.eslint": true },
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"editor.bracketPairColorization.enabled": true,
"editor.guides.bracketPairs": "active",
"[javascript]": {
Expand Down
137 changes: 120 additions & 17 deletions 3-JSKata/1-objects-and-arrays/kata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Comment on lines +135 to +146
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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

‼️ 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.

Suggested change
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
}
function replaceItemAtIndex(arr, idx, item) {
const newArr = [...arr]
if (idx >= 0 && idx < arr.length) {
newArr[idx] = item
}
return newArr
}
🤖 Prompt for AI Agents
In 3-JSKata/1-objects-and-arrays/kata.js around lines 135 to 146, the function
replaceItemAtIndex incorrectly compares array values instead of indices in the
loop, causing multiple replacements if values match. Fix this by changing the
condition to compare the current index i with the target index idx, so only the
element at idx is replaced with item.


// 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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

‼️ 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.

Suggested change
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
}
function insertItemAtIndex(arr, item, idx) {
return [...arr.slice(0, idx), item, ...arr.slice(idx)]
}
🤖 Prompt for AI Agents
In 3-JSKata/1-objects-and-arrays/kata.js lines 150 to 161, the current
insertItemAtIndex function incorrectly modifies the array during iteration and
does not insert the item properly. To fix this, avoid modifying the array inside
the loop; instead, create a new array by slicing the original array up to the
index, then add the new item, and finally concatenate the remaining elements
from the original array after the index. Return this new array as the result.


// 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
Copy link

Choose a reason for hiding this comment

The 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

‼️ 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.

Suggested change
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
}
function deleteItemAtIndex(arr, idx) {
return arr.filter((_, index) => index !== idx)
}
🤖 Prompt for AI Agents
In 3-JSKata/1-objects-and-arrays/kata.js around lines 165 to 176, the function
deleteItemAtIndex modifies the array while iterating, which is inefficient and
error-prone. Instead, create a new array by filtering out the element at the
given index or use array slicing to exclude the item at idx. This will simplify
the logic and avoid modifying the array during iteration.


// 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Function mutates input and uses incorrect splice parameters.

The function has two major issues:

  1. It mutates the original array instead of returning a new one
  2. splice(i, 0, item) inserts the item instead of removing it

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

‼️ 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.

Suggested change
function deleteItem(arr, item) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] == item) {
arr.splice(i, 0, item)
}
}
return arr
}
function deleteItem(arr, item) {
return arr.filter(el => el !== item)
}
🤖 Prompt for AI Agents
In 3-JSKata/1-objects-and-arrays/kata.js around lines 179 to 187, the deleteItem
function incorrectly mutates the input array and uses splice with parameters
that insert the item instead of removing it. To fix this, avoid mutating the
original array by creating and returning a new array that excludes the specified
item, and remove the incorrect splice call that inserts the item. Use a method
like filter to return a new array without the item.


// zipObject should return an object built from two arrays
// For example, given ['foo', 'bar'] and [1, 2] it would return
Expand Down
75 changes: 66 additions & 9 deletions 3-JSKata/2-strings-and-numbers/utilities.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,77 @@
function getType(thing) {}
function getType(thing) {
return typeof thing
}

function isNumber(thing) {
if (typeof thing === 'number') {
return true
} else {
return false
}
}

function toNumber(str) {
return Number(str)
}

function isStringNumber(str) {
if (isNaN(str)) {
return false
} else {
return true
}
}

function isNumber(thing) {}
function add(a, b) {
return a + b
}

function toNumber(str) {}
function addStrings(a, b) {
let num1 = parseInt(a)
let num2 = parseInt(b)
let num3 = num1 + num2

function isStringNumber(str) {}
return String(num3)
}
Comment on lines +29 to +35
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add radix parameter to parseInt calls.

The parseInt function should always include a radix (base) parameter. Without it, strings with leading zeros could be interpreted as octal in older JavaScript environments, leading to unexpected results.

Apply this diff:

 function addStrings(a, b) {
-  let num1 = parseInt(a)
-  let num2 = parseInt(b)
+  let num1 = parseInt(a, 10)
+  let num2 = parseInt(b, 10)
   let num3 = num1 + num2
 
   return String(num3)
 }
📝 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.

Suggested change
function addStrings(a, b) {
let num1 = parseInt(a)
let num2 = parseInt(b)
let num3 = num1 + num2
function isStringNumber(str) {}
return String(num3)
}
function addStrings(a, b) {
let num1 = parseInt(a, 10)
let num2 = parseInt(b, 10)
let num3 = num1 + num2
return String(num3)
}
🤖 Prompt for AI Agents
In 3-JSKata/2-strings-and-numbers/utilities.js around lines 29 to 35, the
parseInt calls omit the radix which can produce incorrect parsing for strings
with leading zeros; update both parseInt(a) and parseInt(b) to include an
explicit radix of 10 (e.g., parseInt(a, 10) and parseInt(b, 10)) so the values
are parsed as decimal before summing and returning the result as a string.


function add(a, b) {}
function addStringsOrNumbers(a, b) {
let num1 = parseInt(a)
let num2 = parseInt(b)
let num3 = num1 + num2

function addStrings(a, b) {}
if (isNumber(a) && isNumber(b)) {
return num3
} else {
return String(num3)
}
}

function addStringsOrNumbers(a, b) {}
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
}
}
}

function isEmail(str) {}
function countIf(array, fn) {
let count = 0

function countIf(array, fn) {}
for (let i = 0; i < array.length; i++) {
if (fn(array[i]) != false) {
count += 1
}
}
return count
}

function filterStringsWithCommas(str) {}

Expand Down
Loading