Skip to content

Commit

Permalink
fix: add some test cases and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
natein committed Dec 29, 2023
1 parent ddc8d09 commit 245ca8c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 30 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ module.exports = {
],
},
plugins: ['prettier'],
noInlineConfig: true,
};
23 changes: 12 additions & 11 deletions src/arrays-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ function getIntervalArray(/* start, end */) {
* @return {array} - An array containing the sum of corresponding elements.
*
* @example
* addArrays([1, 2, 3], [4, 5, 6]) => [5, 7, 9]
* addArrays([10, 20, 30], [5, 10, 15]) => [15, 30, 45]
* addArrays([-1, 0, 1], [1, 2, 3]) => [0, 2, 4]
* sumArrays([1, 2, 3], [4, 5, 6]) => [5, 7, 9]
* sumArrays([10, 20, 30], [5, 10, 15]) => [15, 30, 45]
* sumArrays([-1, 0, 1], [1, 2, 3, 4]) => [0, 2, 4, 4]
*/
function addArrays(/* arr1, arr2 */) {
function sumArrays(/* arr1, arr2 */) {
throw new Error('Not implemented');
}

Expand Down Expand Up @@ -92,7 +92,7 @@ function removeFalsyValues(/* arr */) {
}

/**
* Returns the array of string lengths from the specified string array.
* Returns an array containing the lengths of each string in a specified array of strings.
*
* @param {array} arr - The input array.
* @return {array} - The array of string lengths.
Expand Down Expand Up @@ -252,8 +252,9 @@ function distinct(/* arr */) {
* @return {array} - The n-dimensional array filled with zeros.
*
* @example
* createNDimensionalArray(2, 3) => [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ]
* createNDimensionalArray(3, 2) => [ [ [0, 0], [0, 0] ], [ [0, 0], [0, 0] ] ]
* createNDimensionalArray(2, 3) => [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
* createNDimensionalArray(3, 2) => [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
* createNDimensionalArray(4, 2) => [[[[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]]
* createNDimensionalArray(1, 1) => [0]
*/
function createNDimensionalArray(/* n, size */) {
Expand Down Expand Up @@ -465,9 +466,9 @@ function findCommonElements(/* arr1, arr2 */) {
* @return {number} - The length of the longest increasing subsequence.
*
* @example
* findLongestIncreasingSubsequence([10, 22, 9, 33, 21, 50, 41, 60, 80]) => 6
* findLongestIncreasingSubsequence([3, 10, 2, 1, 20]) => 3
* findLongestIncreasingSubsequence([50, 3, 10, 7, 40, 80]) => 4
* findLongestIncreasingSubsequence([10, 22, 9, 33, 21, 50, 41, 60, 80]) => 3
* findLongestIncreasingSubsequence([3, 10, 2, 1, 20]) => 2
* findLongestIncreasingSubsequence([50, 3, 10, 7, 40, 80]) => 3
*/
function findLongestIncreasingSubsequence(/* nums */) {
throw new Error('Not implemented');
Expand Down Expand Up @@ -550,7 +551,7 @@ function swapHeadAndTail(/* arr */) {

module.exports = {
getIntervalArray,
addArrays,
sumArrays,
findElement,
findAllOccurrences,
removeFalsyValues,
Expand Down
106 changes: 87 additions & 19 deletions test/arrays-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,41 @@ describe('arrays-tasks', () => {
}
);

// addArrays
it.optional(
'sumArrays should return the index of specified value if exists',
() => {
[
{
arr1: [1, 2, 3],
arr2: [4, 5, 6],
expected: [5, 7, 9],
},
{
arr1: [10, 20, 30],
arr2: [5, 10, 15],
expected: [15, 30, 45],
},
{
arr1: [-1, 0, 1],
arr2: [1, 2, 3, 4],
expected: [0, 2, 4, 4],
},
{
arr1: [],
arr2: [1, 2, 3, 4],
expected: [1, 2, 3, 4],
},
{
arr1: [1, 2, 3, 4],
arr2: [],
expected: [1, 2, 3, 4],
},
].forEach((data) => {
const actual = tasks.sumArrays(data.arr1, data.arr2);
assert.deepEqual(data.expected, actual);
});
}
);

it.optional(
'findElement should return the index of specified value if exists',
Expand Down Expand Up @@ -157,6 +191,10 @@ describe('arrays-tasks', () => {
arr: ['angular', 'react', 'ember'],
expected: [7, 5, 5],
},
{
arr: [],
expected: [],
},
].forEach((data) => {
const actual = tasks.getStringsLength(data.arr);
assert.deepEqual(data.expected, actual);
Expand Down Expand Up @@ -200,13 +238,17 @@ describe('arrays-tasks', () => {
() => {
[
{
arr: ['apple', 'banana', 'cherry'],
arr: ['potato', 'banana', 'cherry'],
expected: true,
},
{
arr: ['cat', 'dog', 'elephant'],
expected: false,
},
{
arr: ['cat'],
expected: true,
},
].forEach((data) => {
const actual = tasks.isSameLength(data.arr);
assert.strictEqual(data.expected, actual);
Expand Down Expand Up @@ -271,6 +313,11 @@ describe('arrays-tasks', () => {
n: 3,
expected: ['a', 'b', 'c'],
},
{
arr: ['c', 'a', 't'],
n: 0,
expected: [],
},
].forEach((data) => {
assert.deepEqual(data.expected, tasks.getHead(data.arr, data.n));
});
Expand All @@ -291,6 +338,11 @@ describe('arrays-tasks', () => {
n: 3,
expected: ['b', 'c', 'd'],
},
{
arr: ['c', 'a', 't'],
n: 0,
expected: [],
},
].forEach((data) => {
assert.deepEqual(data.expected, tasks.getTail(data.arr, data.n));
});
Expand Down Expand Up @@ -399,8 +451,34 @@ describe('arrays-tasks', () => {
],
},
{
n: 0,
size: 0,
n: 4,
size: 2,
expected: [
[
[
[0, 0],
[0, 0],
],
[
[0, 0],
[0, 0],
],
],
[
[
[0, 0],
[0, 0],
],
[
[0, 0],
[0, 0],
],
],
],
},
{
n: 1,
size: 1,
expected: [0],
},
].forEach((data) => {
Expand Down Expand Up @@ -689,7 +767,7 @@ describe('arrays-tasks', () => {
},
{
arr: [1, 2],
n: 2,
n: 1,
expected: [2],
},
{
Expand Down Expand Up @@ -757,6 +835,10 @@ describe('arrays-tasks', () => {
arr: [50, 3, 10, 7, 40, 80],
expected: 3,
},
{
arr: [41, 60, 80, 10, 22, 9, 33, 21, 50],
expected: 3,
},
].forEach((data) => {
const actual = tasks.findLongestIncreasingSubsequence(data.arr);
assert.strictEqual(data.expected, actual);
Expand Down Expand Up @@ -953,20 +1035,6 @@ describe('strings-tasks optimal implementation', () => {
);
});

it.optional('optimal implementation of findAllOccurrences', function test() {
let fnStr = tasks.findAllOccurrences.toString();
const idx = fnStr.indexOf('{');
fnStr = fnStr.slice(idx);
if (!fnStr.includes('return')) {
this.skip();
}
assert.equal(
fnStr.includes('find'),
true,
'You need to use a different method, look for the appropriate method in the documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array'
);
});

it.optional('optimal implementation of removeFalsyValues', function test() {
const fnStr = tasks.removeFalsyValues.toString();
if (!fnStr.includes('return')) {
Expand Down

0 comments on commit 245ca8c

Please sign in to comment.