-
Notifications
You must be signed in to change notification settings - Fork 3
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
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,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
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. Add radix parameter to The 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
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
GunnerTeh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
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) {} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
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.
📝 Committable suggestion
🤖 Prompt for AI Agents