├── .gitignore
├── basic
├── 01-hello-world
│ ├── hello-world-run.js
│ ├── hello-world.test.js
│ ├── helloWorld.js
│ └── readme.md
├── 02-get-sum
│ ├── get-sum-run.js
│ ├── get-sum.js
│ ├── get-sum.test.js
│ └── readme.md
├── 03-calculator
│ ├── calculator-run.js
│ ├── calculator.js
│ ├── calculator.test.js
│ └── readme.md
├── 04-count-occurences
│ ├── count-occurences-run.js
│ ├── count-occurences.js
│ ├── count-occurences.test.js
│ └── readme.md
├── 05-find-max-number
│ ├── find-max-run.js
│ ├── find-max.js
│ └── find-max.test.js
├── 06-reverse-a-number
│ ├── reverse_a_number.js
│ ├── reverse_a_number_run.js
│ └── reverse_number.test.js
├── 07-move-zeros-to-end
│ ├── README.md
│ ├── move_zeros_to_end.js
│ ├── move_zeros_to_end.test.js
│ └── move_zeros_to_end_run.js
├── 08-move-array-d-places
│ ├── README.md
│ ├── move_array_d_places.js
│ ├── move_array_d_places.test.js
│ └── move_array_d_places_run.js
├── 09-count-max-number-of-ones
│ ├── count_max_ones.js
│ ├── count_max_ones.test.js
│ └── count_max_ones_run.js
├── 10-union-of-two-sorted-arrays
│ ├── union_of_two_sorted_array.js
│ ├── union_of_two_sorted_array.test.js
│ └── union_of_two_sorted_array_run.js
└── 11-intersection-of-two-sorted-array
│ ├── intersection-of-two-sorted-array.js
│ ├── intersection_of_two_sorted_arrays.test.js
│ └── intersection_of_two_sorted_arrays_run.js
├── hackerank
├── simpleArraySum
│ ├── readme.md
│ └── simpleArraySum.js
└── solveMeFirst
│ ├── READ.md
│ ├── readme.md
│ └── solveMeFirst.js
├── index.txt
├── package-lock.json
├── package.json
├── random
├── filter.js
├── map.js
└── reducer.js
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .DS_Store
--------------------------------------------------------------------------------
/basic/01-hello-world/hello-world-run.js:
--------------------------------------------------------------------------------
1 | const helloWorld = require('./helloWorld');
2 |
3 | const result = helloWorld();
4 |
5 | console.log(result);
6 |
--------------------------------------------------------------------------------
/basic/01-hello-world/hello-world.test.js:
--------------------------------------------------------------------------------
1 | const helloWorld = require('./helloWorld');
2 |
3 | test(`Returning 'Hello World' as a string`, () => {
4 | const result = helloWorld();
5 | expect(result).toBe('Hello world!');
6 | });
7 |
--------------------------------------------------------------------------------
/basic/01-hello-world/helloWorld.js:
--------------------------------------------------------------------------------
1 | function helloWorld() {
2 | return 'Hello world!';
3 | }
4 |
5 | module.exports = helloWorld;
6 |
--------------------------------------------------------------------------------
/basic/01-hello-world/readme.md:
--------------------------------------------------------------------------------
1 | # Challenge: Hello World Sample Challenge
2 |
3 | This is a practice challenge to show you how things are set up and how to test, etc.
4 |
5 | ## Instructions
6 |
7 | Write a function called `helloWorld` that returns a string of 'Hello World!'.
8 |
9 | ### Function Signature
10 |
11 | ```js
12 | /**
13 | * Returns a string containing 'Hello World!'.
14 | * @returns {string} - The string 'Hello World!'.
15 | */
16 | function helloWorld(): string;
17 | ```
18 |
19 | ### Examples
20 |
21 | ```JS
22 | helloWorld() // 'Hello World!'
23 | ```
24 |
25 | ### Constraints
26 |
27 | I will put any constraints here. They will vary depending on the challenge.
28 |
29 | - The function must return a string
30 |
31 | ### Hints
32 |
33 | - I will put a couple hints here. You can choose to use them or not.
34 |
35 | ## Solutions
36 |
37 |
38 | Click For Solution
39 |
40 | ```JS
41 | function printHelloWorld() {
42 | return 'Hello World!';
43 | }
44 | ```
45 |
46 | ### Explanation
47 |
48 | I will put the explanation to the solution here. The length and depth of the explanation will vary depending on the challenge.
49 |
50 |
51 |
52 | ### Test Cases
53 |
54 | The Jest tests will go here. They are already included in the course files. You just need to run `npm test`. Sometimes I will also put manual tests here.
55 |
56 | ```JS
57 | test("Returning 'Hello, World!' as a string", () => {
58 | const result = helloWorld();
59 | expect(result).toBe('Hello World!');
60 | });
61 | ```
62 |
--------------------------------------------------------------------------------
/basic/02-get-sum/get-sum-run.js:
--------------------------------------------------------------------------------
1 | const getSum = require('./get-sum');
2 |
3 | const result = getSum(1, 2);
4 |
5 | console.log(result);
6 |
--------------------------------------------------------------------------------
/basic/02-get-sum/get-sum.js:
--------------------------------------------------------------------------------
1 | function getSum(a, b) {
2 | return a + b;
3 | }
4 |
5 | module.exports = getSum;
6 |
--------------------------------------------------------------------------------
/basic/02-get-sum/get-sum.test.js:
--------------------------------------------------------------------------------
1 | const getSum = require('./get-sum');
2 |
3 | test('should return the sum of two numbers', () => {
4 | const num1 = 1;
5 | const num2 = 2;
6 |
7 | const result = getSum(num1, num2);
8 | expect(result).toBe(3);
9 | });
10 |
--------------------------------------------------------------------------------
/basic/02-get-sum/readme.md:
--------------------------------------------------------------------------------
1 | # Challenge: Get Sum
2 |
3 | This is another very basic practice challenge just to get you into the hang of things.
4 |
5 | ## Instructions
6 |
7 | Write a function called `getSum` that takes in two numbers and returns the sum of those two numbers.
8 |
9 | ### Function Signature
10 |
11 | ```js
12 | /**
13 | * Returns the sum of two numbers.
14 | * @param {number} a - The first number.
15 | * @param {number} b - The second number.
16 | * @returns {number} - The sum of the two numbers.
17 | */
18 | function getSum(a: number, b: number): number;
19 | ```
20 |
21 | ### Examples
22 |
23 | ```JS
24 | getSum(1, 2) // 3
25 | getSum(10, 5) // 15
26 | getSum(2, 2) // 4
27 | getSum(10, 5) // 15
28 | ```
29 |
30 | ### Constraints
31 |
32 | - The function must return a number
33 |
34 | ### Hints
35 |
36 | - You can use the `+` operator to add two numbers together.
37 |
38 | ## Solutions
39 |
40 |
41 | Click For Solution
42 |
43 | ```JS
44 | function getSum(a, b) {
45 | return a + b;
46 | }
47 | ```
48 |
49 | ### Explanation
50 |
51 | This is a pretty simple challenge. We created a function that takes in two values and we added those two values together. We then returned the sum of those two values.
52 |
53 |
54 |
55 | ### Test Cases
56 |
57 | ```JS
58 | test('Calculating the sum of two numbers', () => {
59 | // Test case inputs
60 | const num1 = 5;
61 | const num2 = 7;
62 |
63 | // Call the function
64 | const result = getSum(num1, num2);
65 |
66 | // Check if the result is equal to the expected sum
67 | expect(result).toBe(12);
68 | });
69 | ```
70 |
--------------------------------------------------------------------------------
/basic/03-calculator/calculator-run.js:
--------------------------------------------------------------------------------
1 | const calculator = require('./calculator');
2 |
3 | const result = calculator(19, 4.000899938383, '/');
4 |
5 | console.log(result);
6 |
--------------------------------------------------------------------------------
/basic/03-calculator/calculator.js:
--------------------------------------------------------------------------------
1 | // switch case approach.
2 |
3 | // function calculator(num1, num2, operator) {
4 | // let result;
5 |
6 | // switch (operator) {
7 | // case '+':
8 | // result = num1 + num2;
9 | // break;
10 | // case '-':
11 | // result = num1 - num2;
12 | // break;
13 |
14 | // case '/':
15 | // result = num1 / num2;
16 | // break;
17 | // case '*':
18 | // result = num1 * num2;
19 | // break;
20 |
21 | // default:
22 | // throw new Error('Invalid Operators');
23 | // }
24 | // return result;
25 | // }
26 |
27 | // conditional statements "if" and "else" approach
28 |
29 | function calculator(num1, num2, operator) {
30 | let result;
31 |
32 | if (operator === '+') {
33 | result = num1 + num2;
34 | } else if (operator === '-') {
35 | result = num1 - num2;
36 | } else if (operator === '/') {
37 | result = num1 / num2;
38 | } else if (operator === '*') {
39 | result = num1 * num2;
40 | } else {
41 | throw new Error('Invalid operator');
42 | }
43 |
44 | return result;
45 | }
46 | module.exports = calculator;
47 |
--------------------------------------------------------------------------------
/basic/03-calculator/calculator.test.js:
--------------------------------------------------------------------------------
1 | const calculator = require('./calculator');
2 |
3 | test('should calculate the arithmetic of two inputs data correctly', () => {
4 | const num1 = 7;
5 | const num2 = 2;
6 |
7 | // addition fx
8 | expect(calculator(num1, num2, '+')).toBe(9);
9 |
10 | // subtraction fx
11 | expect(calculator(num1, num2, '-')).toBe(5);
12 |
13 | // multiplication fx
14 |
15 | expect(calculator(num1, num2, '*')).toBe(14);
16 |
17 | // division fx
18 |
19 | expect(calculator(num1, num2, '/')).toBeCloseTo(3.5);
20 | });
21 |
--------------------------------------------------------------------------------
/basic/03-calculator/readme.md:
--------------------------------------------------------------------------------
1 | # Challenge: Calculator
2 |
3 | Write a function called `calculator` that takes in 2 numbers and an operator and returns the result of the calculation.
4 |
5 | ### Function Signature
6 |
7 | ```js
8 | /**
9 | * Returns the result of a calculation.
10 | * @param {number} num1 - The first number.
11 | * @param {number} num2 - The second number.
12 | * @param {string} operator - The operator to use in the calculation.
13 | * @returns {number} - The result of the calculation.
14 | */
15 | function calculator(num1: number, num2: number, operator: string): number;
16 | ```
17 |
18 | ### Examples
19 |
20 | ```JS
21 | calculator(1, 2, '+') // 3
22 | calculator(10, 5, '-') // 5
23 | calculator(2, 2, '*') // 4
24 | calculator(10, 5, '/') // 2
25 | ```
26 |
27 | ### Constraints
28 |
29 | - The function must return a number
30 | - The function must throw or log an error if an invalid operator is given
31 |
32 | ### Hints
33 |
34 | - You can use `if` statements or `switch` statements to determine which operator was given.
35 |
36 | ## Solutions
37 |
38 |
39 | Click For Solution 1
40 |
41 | #### Using a switch:
42 |
43 | ```js
44 | function calculator(num1, num2, operator) {
45 | let result;
46 |
47 | switch (operator) {
48 | case '+':
49 | result = num1 + num2;
50 | break;
51 | case '-':
52 | result = num1 - num2;
53 | break;
54 | case '*':
55 | result = num1 * num2;
56 | break;
57 | case '/':
58 | result = num1 / num2;
59 | break;
60 | default:
61 | throw new Error('Invalid operator');
62 | }
63 |
64 | return result;
65 | }
66 | ```
67 |
68 | ### Explanation
69 |
70 | - Created a function called `calculator` that takes in three arguments: `num1`, `num2`, and `operator`.
71 | - Create a variable called `result` to store the result of the calculation.
72 | - Used a `switch` statement to determine which operator was given. If it was +, -, \* or /, we did the calculation. If the operator is anything else, we throw an error.
73 |
74 |
75 |
76 |
77 | Click For Solution 2
78 |
79 | #### Using an if statement:
80 |
81 | ```js
82 | function calculator(num1, num2, operator) {
83 | let result;
84 |
85 | if (operator === '+') {
86 | result = num1 + num2;
87 | } else if (operator === '-') {
88 | result = num1 - num2;
89 | } else if (operator === '*') {
90 | result = num1 * num2;
91 | } else if (operator === '/') {
92 | result = num1 / num2;
93 | } else {
94 | throw new Error('Invalid operator');
95 | }
96 |
97 | return result;
98 | }
99 | ```
100 |
101 | ### Explanation
102 |
103 | - Create a function called `calculator` that takes in three arguments: `num1`, `num2`, and `operator`.
104 | - Create a variable called `result` to store the result of the calculation.
105 | - Use an `if` statement to determine which operator was given. If it was +, -, \* or /, we did the calculation. If the operator is anything else, we throw an error.
106 |
107 |
108 |
109 | ### Test Cases
110 |
111 | ```js
112 | test('Performing arithmetic operations using the calculator function', () => {
113 | // Test case inputs
114 | const num1 = 5;
115 | const num2 = 7;
116 |
117 | // Addition
118 | expect(calculator(num1, num2, '+')).toBe(12);
119 |
120 | // Subtraction
121 | expect(calculator(num1, num2, '-')).toBe(-2);
122 |
123 | // Multiplication
124 | expect(calculator(num1, num2, '*')).toBe(35);
125 |
126 | // Division
127 | expect(calculator(num1, num2, '/')).toBeCloseTo(0.7143, 4);
128 |
129 | // Invalid operator
130 | expect(() => calculator(num1, num2, '^')).toThrow('Invalid operator');
131 | });
132 | ```
133 |
--------------------------------------------------------------------------------
/basic/04-count-occurences/count-occurences-run.js:
--------------------------------------------------------------------------------
1 | const countOccurrences = require('./count-occurences');
2 |
3 | const result = countOccurrences(111000, 0);
4 |
5 | console.log(result);
6 |
--------------------------------------------------------------------------------
/basic/04-count-occurences/count-occurences.js:
--------------------------------------------------------------------------------
1 | // ? solution 1
2 |
3 | function countOccurrences(str, char) {
4 | let count = 0;
5 |
6 | for (let i = 0; i < str.length; i++) {
7 | if (str[i] === char) {
8 | count++;
9 | }
10 | }
11 |
12 | return count;
13 | }
14 |
15 | // const countOccurrences = (str, char) => {
16 | // return str.split(char).length - 1;
17 | // };
18 |
19 | // insensitive case count
20 |
21 | // function countOccurrences(str, char) {
22 | // const lowerStr = str.toLowerCase();
23 | // const lowerChar = char.toLowerCase();
24 | // let count = 0;
25 |
26 | // for (let i = 0; i < lowerStr.length; i++) {
27 | // if (lowerStr[i] === lowerChar) {
28 | // count++;
29 | // }
30 | // return count;
31 | // }
32 | // }
33 | module.exports = countOccurrences;
34 |
--------------------------------------------------------------------------------
/basic/04-count-occurences/count-occurences.test.js:
--------------------------------------------------------------------------------
1 | const countOccurrences = require('./count-occurences');
2 |
3 | test('should check through input string and check the count of the number of character in the string', () => {
4 | expect(countOccurrences('hello', 'l')).toBe(2);
5 | expect(countOccurrences('Victoria', 'e')).toBe(0);
6 | expect(countOccurrences('bless up', 'u')).toBe(1);
7 | });
8 |
--------------------------------------------------------------------------------
/basic/04-count-occurences/readme.md:
--------------------------------------------------------------------------------
1 | # Challenge: Count Occurrences
2 |
3 | ## Instructions
4 |
5 | Write a function called `countOccurrences()` that takes in a string and a character and returns the number of occurrences of that character in the string.
6 |
7 | ### Function Signature
8 |
9 | ```js
10 | /**
11 | * Returns the number of occurrences of a character in a string.
12 | * @param {string} str - The string to search.
13 | * @param {string} char - The character to search for.
14 | * @returns {number} - The number of occurrences of the character in the string.
15 | */
16 | function countOccurrences(str: string, char: string): number;
17 | ```
18 |
19 | ### Examples
20 |
21 | ```js
22 | countOccurrences('hello', 'l'); // 2
23 | countOccurrences('hello', 'z'); // 0
24 | ```
25 |
26 | ### Constraints
27 |
28 | - Lowercase and uppercase characters are considered different characters. If you want, you can make the function case insensitive
29 |
30 | ### Hints- You can loop through a string just like you can loop through an array.
31 |
32 | - You can use the `++` operator to increment a variable.
33 | - You could take another approach and use the `split()` method to split the string into an array of substrings based on the given character.
34 |
35 | # Note
36 |
37 | this work for both number and string literals.
38 |
--------------------------------------------------------------------------------
/basic/05-find-max-number/find-max-run.js:
--------------------------------------------------------------------------------
1 | const findMaxNunber = require('./find-max');
2 |
3 | const result = findMaxNunber([1, 3, 4, 5, 5, 7, 20]);
4 |
5 | console.log(result);
6 |
7 | // works soo well.
--------------------------------------------------------------------------------
/basic/05-find-max-number/find-max.js:
--------------------------------------------------------------------------------
1 | function findMaxNunber(arr) {
2 | return Math.max(...arr);
3 | }
4 |
5 | module.exports = findMaxNunber;
6 |
--------------------------------------------------------------------------------
/basic/05-find-max-number/find-max.test.js:
--------------------------------------------------------------------------------
1 | const findMaxNunber = require('./find-max');
2 |
3 | test('should log the highest number in the array ', () => {
4 | expect(findMaxNunber([1, 2, 3, 4, 5, 6, 7, 8, 88, 100])).toBe(100);
5 | });
6 |
--------------------------------------------------------------------------------
/basic/06-reverse-a-number/reverse_a_number.js:
--------------------------------------------------------------------------------
1 |
2 | function reverseANumber(num) {
3 | let reversedNumber = 0;
4 |
5 | while (num !== 0) {
6 | //get the last digit of the number
7 | let lastDigit = num % 10;
8 | //get the updated number without the last digit.
9 | num = Math.floor(num / 10);
10 | reversedNumber = (reversedNumber * 10 )+ lastDigit;
11 | }
12 | return reversedNumber;
13 | }
14 |
15 | module.exports = reverseANumber;
--------------------------------------------------------------------------------
/basic/06-reverse-a-number/reverse_a_number_run.js:
--------------------------------------------------------------------------------
1 | const reverseANumber = require("./reverse_a_number");
2 | let num1 = 12345;
3 | let num2 = 246810;
4 | let num3 = 357913117;
5 |
6 |
7 | console.log(`the reverse of ${num1} is: ${reverseANumber(num1)}`);
8 | console.log(`the reverse of ${num2} is: ${reverseANumber(num2)}`);
9 | console.log(`the reverse of ${num3} is: ${reverseANumber(num3)}`);
10 |
--------------------------------------------------------------------------------
/basic/06-reverse-a-number/reverse_number.test.js:
--------------------------------------------------------------------------------
1 | const reverseANumber = require("./reverse_a_number");
2 | let testNum = 4378689736
3 | describe("reverse a number passed to the reverseANumber function", ()=>{
4 | test("should return the reverse of a number", ()=>{
5 | expect(reverseANumber(testNum)).toBe(6379868734);
6 | })
7 | })
--------------------------------------------------------------------------------
/basic/07-move-zeros-to-end/README.md:
--------------------------------------------------------------------------------
1 | ## How does the move_zeros_to_end function work?
2 | ### Steps:
3 |
4 | - We look for the first zero(0) digit and save the index in a variable, say k, which we are going to use later. We initialize the variable to -1(reason for this will be mentioned in the below steps);
5 |
6 | - we check the rest of the dataset, in this case an array, we start the search at an index one greater (k + 1) than the index we found the first zero(0) digit, since we already know that index has a zero digit.
7 |
8 | - Before we start searching the rest of the dataset we check if our variable k is equal to -1. If yes, then it means the dataset does not contain a zero digit in the dataset. We therefore return from the function.
9 |
10 | - We search the rest of the dataset and perform a swap anytime we ecounter a non-zero digit and increment the variable k to hold the current index of the recently swapped zero digit.
11 |
12 | - After the search operation is done the dataset will have all the zero digit(s) moved to the end.
13 |
--------------------------------------------------------------------------------
/basic/07-move-zeros-to-end/move_zeros_to_end.js:
--------------------------------------------------------------------------------
1 |
2 |
3 | function move_zeros_to_end(arr) {
4 | //get the first zero in the array.
5 | let k = -1;
6 | let N = arr.length;
7 | let zero = 0;
8 | for (let i = 0; i < N; ++i) {
9 | if (arr[i] === zero) {
10 | k = i;
11 | //exit from the loop.
12 | break;
13 | }
14 | }
15 | for (let j = k + 1; j < N; ++j) {
16 | if (k === -1) return arr;
17 | //look for a non-zero number.
18 | if (arr[j] !== zero) {
19 | swap(arr, j, k);
20 | ++k;
21 | }
22 | }
23 | return arr;
24 | }
25 |
26 | function swap(arr, idx1, idx2) {
27 | const temp = arr[idx1];
28 | arr[idx1] = arr[idx2];
29 | arr[idx2] = temp;
30 | }
31 |
32 | module.exports = move_zeros_to_end;
--------------------------------------------------------------------------------
/basic/07-move-zeros-to-end/move_zeros_to_end.test.js:
--------------------------------------------------------------------------------
1 | const move_zeros_to_end = require("./move_zeros_to_end");
2 | const arr1 = [1, 2, 0, 0, 3, 4, 5, 6, 7, 8, 0, 0, 15, 12, 13, 0, 3, 4];
3 | const arr2 = [0, 0, 1, 2, 3];
4 | const arr3 = [1, 3, 7, 9, 0, 0, 0]
5 | const arr4 = [11, 3, 44, 2, 5, 7]
6 | const test1 = [1, 3, 7, 9, 0, 0, 0];
7 | const test2 = [1, 2, 3, 0, 0];
8 | const test3 = [1, 2, 3, 4, 5, 6, 7, 8, 15, 12, 13, 3, 4, 0, 0, 0, 0, 0]
9 | describe("move zeros in a dataset to the end", () => {
10 | test("move zero(s) to the end", () => {
11 | expect(move_zeros_to_end(arr1)).toEqual(test3);
12 | expect(move_zeros_to_end(arr2)).toEqual(test2);
13 | expect(move_zeros_to_end(arr3)).toEqual(test1);
14 | })
15 | test("return the intial array, since it contain no zero digit", () => {
16 | expect(move_zeros_to_end(arr4)).toEqual(arr4);
17 | })
18 | })
--------------------------------------------------------------------------------
/basic/07-move-zeros-to-end/move_zeros_to_end_run.js:
--------------------------------------------------------------------------------
1 | const zero_to_end = require("./move_zeros_to_end")
2 |
3 | const arr1 = [1, 2, 0, 0, 3, 4, 5, 6, 7, 8, 0, 0, 15, 12, 13, 0, 3, 4];
4 | const arr2 = [0, 0, 1, 2, 3];
5 | const arr3 = [1, 3, 7, 9, 0, 0, 0]
6 |
7 | console.log(zero_to_end(arr1));
8 | console.log(zero_to_end(arr2));
9 | console.log(zero_to_end(arr3));
--------------------------------------------------------------------------------
/basic/08-move-array-d-places/README.md:
--------------------------------------------------------------------------------
1 | ### How does the function move_array_d_places work?
2 |
3 | - The function moveArrayDplaces() accepts two parmaeters an ```arr```, that would be rotated and a ```number```, that indicates the position the array will be rotated by.
4 | - The array is rotated from left to right, starting from the ```number``` passed as the second parameter.
5 | - We use a modulo operator to ensure that the passed number falls within the index of the array to be rotated.
6 | - The array is returned after it is rotated.
--------------------------------------------------------------------------------
/basic/08-move-array-d-places/move_array_d_places.js:
--------------------------------------------------------------------------------
1 |
2 | function moveArrDplaces(arr, d) {
3 | //clone the arr, so the original array is not mutated.
4 | let clone = arr.slice();
5 |
6 | let n = clone.length;
7 |
8 | //makes the d value to be in the range of 0 to (arr.length - 1).
9 | d = d % n;
10 |
11 | //if the array is moved d-place equal to arr.length, then the array is unchanged.
12 | if (d === clone.length) return clone;
13 |
14 | let temp = []
15 |
16 | //inserting the element to be moved d-places into the temp array.
17 | for (let i = 0; i < d; ++i) {
18 | temp.push(clone[i]);
19 | }
20 |
21 | //moving the rest elements d-places to the left.
22 | for (let j = d; j < n; ++j) {
23 | clone[j - d] = clone[j];
24 | }
25 |
26 | //the index to start the insertion of the elements into the clone array.
27 | let m = n - d;
28 |
29 | for (let k = 0; k < temp.length; ++k) {
30 | clone[m++] = temp[k];
31 | }
32 |
33 | return clone;
34 | }
35 |
36 |
37 | module.exports = moveArrDplaces;
--------------------------------------------------------------------------------
/basic/08-move-array-d-places/move_array_d_places.test.js:
--------------------------------------------------------------------------------
1 | const moveArrDplaces = require("./move_array_d_places");
2 |
3 | const testArr = [1, 2, 3, 4, 5, 6]
4 | const arrMoved1place = [2, 3, 4, 5, 6, 1]
5 | const arrMoved2place = [3, 4, 5, 6, 1, 2]
6 | const arrMoved3place = [4, 5, 6, 1, 2, 3]
7 | const arrMoved4place = [5, 6, 1, 2, 3, 4]
8 | const arrMoved5place = [6, 1, 2, 3, 4, 5]
9 | const arrMoved6place = [1, 2, 3, 4, 5, 6]
10 |
11 | describe('to move an array d-places', () => {
12 | it("to move array 1 place", () => {
13 | expect(moveArrDplaces(testArr, 1)).toEqual(arrMoved1place);
14 | })
15 |
16 | it("to move array 2 place", () => {
17 | expect(moveArrDplaces(testArr, 2)).toEqual(arrMoved2place);
18 | })
19 |
20 | it("to move array 3 place", () => {
21 | expect(moveArrDplaces(testArr, 3)).toEqual(arrMoved3place);
22 | })
23 |
24 | it("to move array 4 place", () => {
25 | expect(moveArrDplaces(testArr, 4)).toEqual(arrMoved4place);
26 | })
27 |
28 | it("to move array 5 place", () => {
29 | expect(moveArrDplaces(testArr, 5)).toEqual(arrMoved5place);
30 | })
31 |
32 | it("to move array 6 place", () => {
33 | expect(moveArrDplaces(testArr, 6)).toEqual(arrMoved6place);
34 | })
35 | })
36 |
--------------------------------------------------------------------------------
/basic/08-move-array-d-places/move_array_d_places_run.js:
--------------------------------------------------------------------------------
1 | const moveArrDplaces = require("./move_array_d_places");
2 |
3 | let arr = [1, 2, 3, 4, 5, 6]
4 | let n = arr.length;
5 |
6 | for (let i = 0 + 1; i < n; ++i) {
7 | //moving the array d-places.
8 | let d = i;
9 | console.log(`The arr ${JSON.stringify(arr)} moved ${d}-place is: ${JSON.stringify(moveArrDplaces(arr, d))}`);
10 | }
11 |
--------------------------------------------------------------------------------
/basic/09-count-max-number-of-ones/count_max_ones.js:
--------------------------------------------------------------------------------
1 |
2 |
3 | function count_max_ones(arr) {
4 | let count = 0;
5 | let max = 0;
6 | for (let i = 0; i < arr.length; ++i) {
7 | if (arr[i] === 1) {
8 | //increment count
9 | ++count;
10 |
11 | //max holds the max number of ones visited yet.
12 | max = Math.max(count, max);
13 |
14 | } else {
15 | //reset count to zero.
16 | count = 0;
17 | }
18 |
19 | }
20 | return max;
21 | }
22 |
23 |
24 | module.exports = count_max_ones;
25 |
--------------------------------------------------------------------------------
/basic/09-count-max-number-of-ones/count_max_ones.test.js:
--------------------------------------------------------------------------------
1 | const count_max_ones = require("./count_max_ones");
2 |
3 | const arr1 = [1, 1, 0, 0, 1, 1, 1];
4 | const arr2 = [1, 1, 1, 1, 0, 0, 1, 1];
5 | const arr3 = [1, 1, 1, 1, 1, 1, 0, 0, 1, 1];
6 | const arr4 = [1, 1, 0, 0, 0, 1];
7 |
8 | describe("count maximun ones in an array", () => {
9 | it("maximum numbero of ones should be 1", () => {
10 | expect(count_max_ones(arr1)).toBe(3);
11 | })
12 | it("maximum number of ones should be 4", () => {
13 | expect(count_max_ones(arr2)).toBe(4);
14 | })
15 | it("maximum number of ones should be 6", () => {
16 | expect(count_max_ones(arr3)).toBe(6);
17 | })
18 | it("maximum number of ones should be 2", ()=>{
19 | expect(count_max_ones(arr4)).toBe(2);
20 | })
21 | })
--------------------------------------------------------------------------------
/basic/09-count-max-number-of-ones/count_max_ones_run.js:
--------------------------------------------------------------------------------
1 | const count_max_ones = require("./count_max_ones");
2 |
3 | const testArr = [
4 | [1, 1, 0, 0, 0, 1, 1, 1],
5 | [1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1],
6 | [1, 0, 1, 1, 1, 1, 1],
7 | [1, 1, 1, 1, 0, 0, 1, 1]
8 | ]
9 |
10 | testArr.forEach(arr => {
11 | console.log(`max number of ones in the array: ${JSON.stringify(arr)} -> `, count_max_ones(arr));
12 | })
--------------------------------------------------------------------------------
/basic/10-union-of-two-sorted-arrays/union_of_two_sorted_array.js:
--------------------------------------------------------------------------------
1 |
2 |
3 | function union_of_two_sorted_array(arr1, arr2) {
4 | let m = arr1.length;
5 | let n = arr2.length;
6 |
7 | let i = 0;
8 | let j = 0;
9 |
10 | let union = [];
11 |
12 | while (i < m && j < n) {
13 | if (arr1[i] < arr2[j]) {
14 | //push to the union array if it is empty or if the last element in the array is not equal to the arr1[i]
15 | if (union.length === 0 || union[union.length - 1] !== arr1[i]) {
16 | union.push(arr1[i]);
17 | }
18 | //increment the variable i, if the arr1[i] is pushed to the union array or not.
19 | ++i;
20 | }
21 | else if (arr2[j] < arr1[i]) {
22 | //push to the union array if it is empty or if the last element in the array is not equal to the arr2[j]
23 | if (union.length === 0 || union[union.length - 1] !== arr2[j]) {
24 | union.push(arr2[j]);
25 | }
26 | //increment the variable i, if the arr1[i] is pushed to the union array or not.
27 | ++j;
28 | }
29 | else {
30 | //push the arr1[i] or arr2[j] and increment the corresponding variable i or j , since either arr1[1] or arr2[j] are equal.
31 | union.push(arr1[i])
32 | ++i;
33 | }
34 | }
35 |
36 |
37 | //runs while there is still element in arr1.
38 | while (i < m) {
39 | if (union.length === 0 || union[union.length - 1] !== arr1[i]) {
40 | union.push(arr1[i])
41 | }
42 | ++i;
43 | }
44 |
45 | //runs while there is still element in arr2.
46 | while (j < n) {
47 | if (union.length === 0 || union[union.length - 1] !== arr2[j]) {
48 | union.push(arr2[j])
49 | }
50 | ++j;
51 | }
52 |
53 | return union;
54 | }
55 |
56 | module.exports = union_of_two_sorted_array
57 |
--------------------------------------------------------------------------------
/basic/10-union-of-two-sorted-arrays/union_of_two_sorted_array.test.js:
--------------------------------------------------------------------------------
1 | const union_of_two_sorted_array = require("./union_of_two_sorted_array");
2 |
3 | const arr1 = [1, 3, 5, 7, 9, 11];
4 | const arr2 = [0, 2, 4, 6, 8, 10];
5 | const arr3 = [13, 15, 17, 19, 21, 23, 27, 29];
6 | const arr4 = [12, 14, 16, 18, 20, 25, 26, 30];
7 |
8 | const union1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
9 | const union2 = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 25, 26, 27, 29, 30]
10 | describe("should get the union of two array", () => {
11 | it(`get the union of arr1: ${JSON.stringify(arr1)} and arr2: ${JSON.stringify(arr2)}`, () => {
12 | expect(union_of_two_sorted_array(arr1, arr2)).toEqual(union1);
13 | })
14 | it(`get the union of arr1: ${JSON.stringify(arr2)} and arr2: ${JSON.stringify(arr2)}`, () => {
15 | expect(union_of_two_sorted_array(arr3, arr4)).toEqual(union2);
16 | })
17 | })
--------------------------------------------------------------------------------
/basic/10-union-of-two-sorted-arrays/union_of_two_sorted_array_run.js:
--------------------------------------------------------------------------------
1 | const union_of_two_sorted_array = require("./union_of_two_sorted_array");
2 |
3 | const testArr1 = [[1, 3, 5, 7, 9, 11], [0, 2, 4, 6, 8, 10]];
4 | const testArr2 = [[13, 15, 17, 19, 21, 23, 27, 29], [12, 14, 16, 18, 20, 25, 26, 30]]
5 |
6 |
7 | console.log(`the union of testArr1: ${JSON.stringify(testArr1)} is: `, union_of_two_sorted_array(testArr1[0], testArr1[1]));
8 | console.log(`the union of testArr2: ${JSON.stringify(testArr2)} is: `, union_of_two_sorted_array(testArr2[0], testArr2[1]));
9 |
--------------------------------------------------------------------------------
/basic/11-intersection-of-two-sorted-array/intersection-of-two-sorted-array.js:
--------------------------------------------------------------------------------
1 |
2 | function intersection_of_two_sorted_array(arr1, arr2) {
3 | let n = arr1.length;
4 | let m = arr2.length;
5 |
6 | let i = 0;
7 | let j = 0;
8 |
9 | let intersection = [];
10 |
11 | while (i < n && j < m) {
12 | if (arr1[i] < arr2[j]) {
13 | //increment the variable i since arr1[i] is smaller, since only bigger values can be found moving toward the right of the arr1
14 | ++i
15 | } else if (arr2[j] < arr1[i]) {
16 | //increment the variable j since arr2[j] is smaller, since only bigger values can be found moving toward the right of the arr2.
17 | ++j
18 | } else {
19 | //add to the intersection array either of the arr1[i] or arr2[j] element and increment both pointers.
20 | intersection.push(arr1[i]);
21 | ++i;
22 | ++j;
23 | }
24 | }
25 |
26 | return intersection;
27 | }
28 |
29 | module.exports = intersection_of_two_sorted_array
30 |
--------------------------------------------------------------------------------
/basic/11-intersection-of-two-sorted-array/intersection_of_two_sorted_arrays.test.js:
--------------------------------------------------------------------------------
1 | const intersection_of_two_sorted_arrays = require("./intersection-of-two-sorted-array");
2 |
3 | const arr1 = [1, 2, 3, 4, 5, 6, 7, 7, 7, 8];
4 | const arr2 = [2, 4, 5, 7, 8];
5 |
6 | const intersection = [2, 4, 5, 7, 8];
7 |
8 | describe("get the intersection of two sorted array", () => {
9 | it(`get the union of arr1: ${JSON.stringify(arr1)} and arr2:${JSON.stringify(arr2)}`, () => {
10 | expect(intersection_of_two_sorted_arrays(arr1,arr2)).toEqual(intersection);
11 | })
12 | })
--------------------------------------------------------------------------------
/basic/11-intersection-of-two-sorted-array/intersection_of_two_sorted_arrays_run.js:
--------------------------------------------------------------------------------
1 | const intersection_of_two_sorted_arrays = require("./intersection-of-two-sorted-array");
2 |
3 | const arr1 = [0, 3, 5, 6, 8, 11, 13, 14, 15, 19];
4 | const arr2 = [0,1, 3, 8, 13, 19];
5 |
6 | console.log(`The intersection of arr1: ${JSON.stringify(arr1)} and arr2: ${JSON.stringify(arr2)} is: `, intersection_of_two_sorted_arrays(arr1, arr2));
--------------------------------------------------------------------------------
/hackerank/simpleArraySum/readme.md:
--------------------------------------------------------------------------------
1 | Given an array of integers, find the sum of its elements.
2 |
3 | For example, if the array , , so return .
4 |
5 | Function Description
6 |
7 | Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.
8 |
9 | simpleArraySum has the following parameter(s):
10 |
11 | ar: an array of integers
12 | Input Format
13 |
14 | The first line contains an integer, , denoting the size of the array.
15 | The second line contains space-separated integers representing the array's elements.
16 |
17 | Constraints
18 |
19 | Output Format
20 |
21 | Print the sum of the array's elements as a single integer.
22 |
23 | Sample Input
24 |
25 | 6
26 | 1 2 3 4 10 11
27 | Sample Output
28 |
29 | 31
30 | Explanation
31 |
32 | We print the sum of the array's elements: .
33 |
34 | 1 + 2 +3+ 4+ 10 + 11 =31
35 |
--------------------------------------------------------------------------------
/hackerank/simpleArraySum/simpleArraySum.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Complete the 'simpleArraySum' function below.
3 | *
4 | * The function is expected to return an INTEGER.
5 | * The function accepts INTEGER_ARRAY ar as parameter.
6 | */
7 |
8 | function simpleArraySum(ar) {
9 | // Write your code here
10 | let total = 0;
11 |
12 | for (const num of ar) {
13 | total += num;
14 | }
15 |
16 | return total;
17 | }
--------------------------------------------------------------------------------
/hackerank/solveMeFirst/READ.md:
--------------------------------------------------------------------------------
1 | ## HOW THE PROGRAM WORKS
2 | - Enter each number by typing a number literal and hitting enter or return.
3 | - Type "calculate" to prompt the program to calculate the final sum of all the numbers previoulsy entered.
4 |
5 | ### FEATURES
6 | - handle invalid input.
--------------------------------------------------------------------------------
/hackerank/solveMeFirst/readme.md:
--------------------------------------------------------------------------------
1 | Complete the function solveMeFirst to compute the sum of two integers.
2 |
3 | Example
4 |
5 | Return 10.
6 |
7 | Function Description
8 |
9 | Complete the solveMeFirst function in the editor below.
10 |
11 | solveMeFirst has the following parameters:
12 |
13 | int a: the first value
14 | int b: the second value
15 | Returns
16 |
17 | - int: the sum of and
18 |
19 | Constraints
20 |
21 | Sample Input
22 |
23 | a = 2
24 | b = 3
25 | Sample Output
26 |
27 | 5
28 | Explanation
29 |
--------------------------------------------------------------------------------
/hackerank/solveMeFirst/solveMeFirst.js:
--------------------------------------------------------------------------------
1 | //this module enable us to read input from the terminal;
2 | const readline = require("node:readline");
3 | const { stdin: input, stdout: output } = require("node:process");
4 | const rl = readline.createInterface({ input, output });
5 |
6 | function calculateSum(...args) {
7 | let numArray = [];
8 | let sum = 0;
9 | args.forEach(num => {
10 | //convert invalid input to zero.
11 | num = parseFloat(num, 10) || 0;
12 | if (num !== 0) numArray.push(num);
13 | sum += num;
14 | })
15 | return { sum: sum, array: numArray.join(" + ") }
16 |
17 | }
18 | const message = ` HOW THE PROGRAM WORKS\n1. Enter the series of number you want to calculate their sum.\n2. Type "calculate" or "CALCULATE" to prompt the program to calculate the final sum.\nEnter number:`
19 |
20 | console.log(message);
21 | const inputArray = [];
22 | //NOTE: input entered into the terminal are parsed as string.
23 | rl.on("line", userInput => {
24 | userInput = userInput.toLowerCase();
25 | let shouldCalculate = userInput === "calculate" ? true : false;
26 |
27 | if (shouldCalculate) {
28 | const {sum, array} = calculateSum(...inputArray);
29 | console.log(`THE SUM OF `, array, " = ", sum);
30 | rl.close();
31 | }
32 |
33 | inputArray.push(userInput);
34 | })
--------------------------------------------------------------------------------
/index.txt:
--------------------------------------------------------------------------------
1 | init () -> January 4th, 2024.
2 |
3 |
4 | Let's become a better developer!⚡
5 |
6 | Questions?
7 |
8 |
9 | Fuck my phone on spoil.
10 |
11 |
12 | fuck my pc spoilt now
13 |
14 |
15 | lmao
16 |
17 | brooo
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "panthreon",
3 | "version": "1.0.0",
4 | "description": "becoming a better and world class software engineer.",
5 | "scripts": {
6 | "test": "jest"
7 | },
8 | "author": "VickyJay",
9 | "license": "MIT",
10 | "devDependencies": {
11 | "jest": "^29.6.1"
12 | },
13 | "dependencies": {
14 | "-": "^0.0.1",
15 | "npm": "^10.3.0"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/random/filter.js:
--------------------------------------------------------------------------------
1 | const numbers = [1, -1, 2, -2, 3, 4];
2 |
3 | // filter only the positive numbers
4 |
5 | const filteredPositive = numbers.filter((num) => num >= 0);
6 |
7 | console.log(filteredPositive);
8 |
9 | // filter only the negative numbers
10 |
11 | const filteredNegative = numbers.filter((num) => {
12 | return num <= 0;
13 | });
14 |
15 | console.log(filteredNegative);
16 |
--------------------------------------------------------------------------------
/random/map.js:
--------------------------------------------------------------------------------
1 | const numbers = [1, -1, 2, -2, 3, 4];
2 |
3 | // filter only the positive numbers
4 |
5 | const filteredPositive = numbers.filter((num) => {
6 | return num >= 0;
7 | });
8 | // console.log(filteredPositive)
9 |
10 | // here we are using the same results from the array we filtered and map through it to display a list item
11 | // NOTE filter and map method doesn't override the initial array
12 | const items = filteredPositive.map((item) => `
${item} `).join("");
13 |
14 | // console.log(items);
15 |
16 | // WITH OBJECTS
17 | const objects = filteredPositive.map((value) => ({ value }));
18 | console.log(objects);
19 |
20 | // CHAINING
21 |
22 | // NOTE - you can use the chaining pattern to just map and filter the same array simultaneously. this is possible because the two methods doesn't mutate the original array
23 |
24 | const chain = numbers
25 | .filter((num) => num > 0)
26 | .map((item) => `${item}`)
27 | .join(" ");
28 | // you can continue to use the methods and play around
29 |
30 | console.log(chain);
31 |
--------------------------------------------------------------------------------
/random/reducer.js:
--------------------------------------------------------------------------------
1 | const numbers = [1, -1, 2, -2, 3, 4];
2 |
3 | // NOTE things happening behind the scene - add the acc value to to the value
4 |
5 | // lets shorten acc = a and cur = c
6 | // a = 0, c = 1 => a = 1
7 | // a = 1, c = 0 => a = 0
8 | // a = 0, c = 2 => 1 a= 0
9 |
10 | // the first four values on the array will yield 0 so we can add the last two and get 7
11 |
12 | const sum = numbers.reduce((acc, cur) => {
13 | return acc + cur;
14 | }, 0);
15 |
16 | console.log(sum);
17 |
18 | // acc = accumulator
19 | // cur = current value
20 |
21 |
22 | //cc Mosh
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # JS Challenges, Data Structures, and Algorithms.👩💻
2 |
3 | - Learn to solve problems with code, write efficient codes!
4 |
5 | # Questions🤔💭
6 |
7 | - Does the time taken for a code to run depend on the processor of the device used to code?
8 |
9 | # Setup🚀
10 |
11 | 1. Clone this repository to your local machine using `git clone`.
12 | 2. Navigate to the project directory: `cd panthreon`.
13 | 3. Install project dependencies using `npm install`.
14 | 4. Start the development server: `npm test`.
15 | 5. install [Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) extension on your vscode.
16 |
17 | # Usage👩🏭
18 | Im pretty sure the files and folders are easy to naviagte and read, each codes are well documented and you have idea of what is really going on, feel free to ask questions too
19 |
20 | # Contribution 🛠
21 | Feel free to contribute to this repository, the little idea and correction is high appreciated, i promise your pull request will be accepted and merged.
22 |
--------------------------------------------------------------------------------