Completed 1-4

This commit is contained in:
Thuan Bui
2025-02-22 10:23:40 +09:00
parent d4421b055f
commit c415382137
7 changed files with 68 additions and 23 deletions
+35 -1
View File
@@ -1,4 +1,38 @@
const removeFromArray = function() {
const removeFromArray = function(originalArray,...theArgs) {
let newArray = [];
console.log("Original Array: " + originalArray)
console.log("Argument Length: " + theArgs.length)
/*for (const number of originalArray) {
console.log("Current number: " + number)
for (let i=0; i < arguments.length; i++) {
console.log("Current argument value: " + theArgs[i]);
if (number === theArgs[i]) {
const index = originalArray.indexOf(number);
if (index > -1) { // only splice array when item is found
originalArray.splice(index, 1); // 2nd parameter means remove one item only
}
console.log(newArray)
}
}
}*/
for (let i=0; i < theArgs.length; i++) {
for (z=0; z < originalArray.length; z++ ) {
//for (const number of originalArray) {
number = originalArray[z];
console.log("Current number: " + number);
if (number === theArgs[i]) {
const index = originalArray.indexOf(number);
if (index > -1) { // only splice array when item is found
originalArray.splice(index, 1); // 2nd parameter means remove one item only
z--;
}
}
console.log(originalArray)
}
}
return originalArray;
};
// Do not edit below this line
+7 -7
View File
@@ -4,25 +4,25 @@ describe('removeFromArray', () => {
test('removes a single value', () => {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
});
test.skip('removes multiple values', () => {
test('removes multiple values', () => {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
});
test.skip('removes multiple of the same value', () => {
test('removes multiple of the same value', () => {
expect(removeFromArray([1, 2, 2, 3], 2)).toEqual([1, 3]);
});
test.skip('ignores non present values', () => {
test('ignores non present values', () => {
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
});
test.skip('ignores non present values, but still works', () => {
test('ignores non present values, but still works', () => {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
});
test.skip('can remove all values', () => {
test('can remove all values', () => {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
});
test.skip('works with strings', () => {
test('works with strings', () => {
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
});
test.skip('only removes same type', () => {
test('only removes same type', () => {
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
});
});