├── reanpx
├── src
│ ├── react-app-env.d.ts
│ ├── setupTests.ts
│ ├── App.test.tsx
│ ├── App.tsx
│ ├── index.css
│ ├── reportWebVitals.ts
│ ├── index.tsx
│ ├── App.css
│ ├── logo.svg
│ └── components
│ │ └── progress-bars
│ │ ├── workingcode.jsx
│ │ └── index.tsx
├── public
│ ├── robots.txt
│ ├── favicon.ico
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── index.html
├── .gitignore
├── tsconfig.json
├── package.json
└── README.md
├── algorithms_based_questions
├── q-3-find-the-missing-number.js
├── q-2_remove_k_element_to_maxiumize_sum.js
└── Q-1_number-of-islands_medium.js
├── math
└── Q-1_fizz-buzz_easy.js
├── data-structures_based_questions
└── Q-1_find_minimum_time_to_make_all_leaf_node_wet.js
└── README.md
/reanpx/src/react-app-env.d.ts:
--------------------------------------------------------------------------------
1 | ///
2 |
--------------------------------------------------------------------------------
/reanpx/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/reanpx/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/HEAD/reanpx/public/favicon.ico
--------------------------------------------------------------------------------
/reanpx/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/HEAD/reanpx/public/logo192.png
--------------------------------------------------------------------------------
/reanpx/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/HEAD/reanpx/public/logo512.png
--------------------------------------------------------------------------------
/reanpx/src/setupTests.ts:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------
/reanpx/src/App.test.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { render, screen } from '@testing-library/react';
3 | import App from './App';
4 |
5 | test('renders learn react link', () => {
6 | render();
7 | const linkElement = screen.getByText(/learn react/i);
8 | expect(linkElement).toBeInTheDocument();
9 | });
10 |
--------------------------------------------------------------------------------
/reanpx/src/App.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import logo from './logo.svg';
3 | import './App.css';
4 | import ProgressBars from './components/progress-bars';
5 |
6 | function App() {
7 | return (
8 |
11 | );
12 | }
13 |
14 | export default App;
15 |
--------------------------------------------------------------------------------
/reanpx/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/reanpx/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/reanpx/src/reportWebVitals.ts:
--------------------------------------------------------------------------------
1 | import { ReportHandler } from 'web-vitals';
2 |
3 | const reportWebVitals = (onPerfEntry?: ReportHandler) => {
4 | if (onPerfEntry && onPerfEntry instanceof Function) {
5 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
6 | getCLS(onPerfEntry);
7 | getFID(onPerfEntry);
8 | getFCP(onPerfEntry);
9 | getLCP(onPerfEntry);
10 | getTTFB(onPerfEntry);
11 | });
12 | }
13 | };
14 |
15 | export default reportWebVitals;
16 |
--------------------------------------------------------------------------------
/algorithms_based_questions/q-3-find-the-missing-number.js:
--------------------------------------------------------------------------------
1 | // Question: https://www.geeksforgeeks.org/find-the-missing-number/
2 | // LeetCode: https://leetcode.com/problems/missing-number/submissions/
3 |
4 | /**
5 | * Time complexity: O(n)
6 | * Space Complexity: O(1)
7 | */
8 | function FindMissingNumber(arr) {
9 | if (!arr) {
10 | return;
11 | }
12 |
13 | const size = arr.length;
14 | const sumOfNNumber = (size * (size + 1))/2;
15 |
16 | const sumOfElementsOfArr = arr.reduce((sum, curr)=> sum + curr,0) ;
17 |
18 | return sumOfNNumber - sumOfElementsOfArr;
19 | }
--------------------------------------------------------------------------------
/reanpx/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/reanpx/src/index.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom/client';
3 | import './index.css';
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 |
7 | const root = ReactDOM.createRoot(
8 | document.getElementById('root') as HTMLElement
9 | );
10 | root.render(
11 |
12 |
13 |
14 | );
15 |
16 | // If you want to start measuring performance in your app, pass a function
17 | // to log results (for example: reportWebVitals(console.log))
18 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
19 | reportWebVitals();
20 |
--------------------------------------------------------------------------------
/reanpx/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "es5",
4 | "lib": [
5 | "dom",
6 | "dom.iterable",
7 | "esnext"
8 | ],
9 | "allowJs": true,
10 | "skipLibCheck": true,
11 | "esModuleInterop": true,
12 | "allowSyntheticDefaultImports": true,
13 | "strict": true,
14 | "forceConsistentCasingInFileNames": true,
15 | "noFallthroughCasesInSwitch": true,
16 | "module": "esnext",
17 | "moduleResolution": "node",
18 | "resolveJsonModule": true,
19 | "isolatedModules": true,
20 | "noEmit": true,
21 | "jsx": "react-jsx"
22 | },
23 | "include": [
24 | "src"
25 | ]
26 | }
27 |
--------------------------------------------------------------------------------
/reanpx/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | pointer-events: none;
8 | }
9 |
10 | @media (prefers-reduced-motion: no-preference) {
11 | .App-logo {
12 | animation: App-logo-spin infinite 20s linear;
13 | }
14 | }
15 |
16 | .App-header {
17 | background-color: #282c34;
18 | min-height: 100vh;
19 | display: flex;
20 | flex-direction: column;
21 | align-items: center;
22 | justify-content: center;
23 | font-size: calc(10px + 2vmin);
24 | color: white;
25 | }
26 |
27 | .App-link {
28 | color: #61dafb;
29 | }
30 |
31 | @keyframes App-logo-spin {
32 | from {
33 | transform: rotate(0deg);
34 | }
35 | to {
36 | transform: rotate(360deg);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/math/Q-1_fizz-buzz_easy.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Problem Statement link: https://leetcode.com/problems/fizz-buzz/
3 | * Level: easy
4 | * Tags: Google
5 | * ETC: 2-3 mins
6 | */
7 |
8 |
9 | /**
10 | * @param {number} n
11 | * @return {string[]}
12 | */
13 | var fizzBuzz = function(n) {
14 | const mapping = {
15 | 3: 'Fizz',
16 | 5: 'Buzz',
17 | 15: 'FizzBuzz'
18 | };
19 |
20 | const ans =[]
21 |
22 | let i=1;
23 | while(i<= n) {
24 | if (i%15 === 0) {
25 | ans.push(mapping[15]);
26 | } else if (i%5 === 0) {
27 | ans.push(mapping[5]);
28 | } else if (i%3 === 0) {
29 | ans.push(mapping[3]);
30 | } else {
31 | ans.push(i.toString());
32 | }
33 | i++;
34 | };
35 |
36 | return ans;
37 | };
--------------------------------------------------------------------------------
/algorithms_based_questions/q-2_remove_k_element_to_maxiumize_sum.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Time Complexity: O(k)
3 | */
4 |
5 | // Remove k elements from array to maximise the sum of removed elements.
6 | // you cam remove only from start or from end.
7 |
8 | function maximiseSumOfKRemoveElements (arr = [], k) {
9 | if (arr.length === 0) return;
10 |
11 | let start = 0;
12 | let end = 0;
13 |
14 | let maxSum = -INFINITE;
15 |
16 | let currSum = 0;
17 |
18 | while(start < k) {
19 | currSum = currSum = arr[start];
20 | start++;
21 | }
22 |
23 | start = start - 1;
24 |
25 | if (currSum > maxSum) maxSum = currSum;
26 | // keep removing last element from left and keep adding right one to sum
27 | while(end < k) {
28 | currSum = currSum - arr[start] + arr[arr.length - 1 - end];
29 |
30 | if (currSum > maxSum) maxSum = currSum;
31 | end++;
32 | start--;
33 | };
34 |
35 | return maxSum;
36 |
37 | }
--------------------------------------------------------------------------------
/reanpx/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "reanpx",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.16.5",
7 | "@testing-library/react": "^13.4.0",
8 | "@testing-library/user-event": "^13.5.0",
9 | "@types/jest": "^27.5.2",
10 | "@types/node": "^16.18.38",
11 | "@types/react": "^18.2.14",
12 | "@types/react-dom": "^18.2.6",
13 | "react": "^18.2.0",
14 | "react-dom": "^18.2.0",
15 | "react-scripts": "5.0.1",
16 | "typescript": "^4.9.5",
17 | "web-vitals": "^2.1.4"
18 | },
19 | "scripts": {
20 | "start": "react-scripts start",
21 | "build": "react-scripts build",
22 | "test": "react-scripts test",
23 | "eject": "react-scripts eject"
24 | },
25 | "eslintConfig": {
26 | "extends": [
27 | "react-app",
28 | "react-app/jest"
29 | ]
30 | },
31 | "browserslist": {
32 | "production": [
33 | ">0.2%",
34 | "not dead",
35 | "not op_mini all"
36 | ],
37 | "development": [
38 | "last 1 chrome version",
39 | "last 1 firefox version",
40 | "last 1 safari version"
41 | ]
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/algorithms_based_questions/Q-1_number-of-islands_medium.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Problem Statement link: https://leetcode.com/problems/number-of-islands/submissions/
3 | * Level: Easy
4 | * Tags: Google, Backtracking
5 | * ETC: 5-7 mins
6 | * Concept covered: backtracking in graphs
7 | */
8 |
9 | /**
10 | *
11 | * @param {Array} grid its a 2d array
12 | * @param {Number} r number of row
13 | * @param {Number} c number of columns
14 | */
15 | function dfs(grid, r, c) {
16 | let nr = grid.length;
17 | let nc = grid[0].length;
18 |
19 | grid[r][c] = '0';
20 | if (r - 1 >= 0 && grid[r-1][c] == '1') dfs(grid, r - 1, c);
21 | if (r + 1 < nr && grid[r+1][c] == '1') dfs(grid, r + 1, c);
22 | if (c - 1 >= 0 && grid[r][c-1] == '1') dfs(grid, r, c - 1);
23 | if (c + 1 < nc && grid[r][c+1] == '1') dfs(grid, r, c + 1);
24 | }
25 | var numIslands = function(grid) {
26 | let nr = grid.length;
27 | if (!nr) return 0;
28 | let nc = grid[0].length;
29 |
30 | let num_islands = 0;
31 | for (let r = 0; r < nr; ++r) {
32 | for (let c = 0; c < nc; ++c) {
33 | if (grid[r][c] == '1') {
34 | ++num_islands;
35 | dfs(grid, r, c);
36 | }
37 | }
38 | }
39 |
40 | return num_islands;
41 | };
--------------------------------------------------------------------------------
/data-structures_based_questions/Q-1_find_minimum_time_to_make_all_leaf_node_wet.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Problem Statement: Not available anywhere
3 | * Level: Medium
4 | * Tags: Google, dfs, minheap
5 | * ETC: 5-7 mins
6 | * Concept covered: DFS, Min Heap
7 | * Time Complexity: first one: O(n), followup: nO(Logn), inserting n nodes where each one takes logn so nlogn, extracting is logn
8 | */
9 |
10 | // function node() {
11 | // this.name = '';
12 | // this.childrens = [{
13 | // node: {}, time: 0
14 | // },...];
15 | // }
16 |
17 | funtion minimumTimeToMakeAllLeafNodeWet(root) {
18 | const minTime = {
19 | time: -INFINITE;
20 | }
21 |
22 | minTimeLeafNodes(root, minTime);
23 | }
24 |
25 | function minTimeLeafNodes(root, minTime, currNodeTimeTaken) {
26 | if (!root) return;
27 |
28 | // leaf node check
29 | if (root.childrens.length === 0) {
30 | if (currNodeTimeTaken > minTime.time) {
31 | minTime.time = currNodeTimeTaken;
32 | }
33 | return;
34 | }
35 |
36 | root.childrens.forEach((node) => {
37 | minTimeLeafNodes(node, minTime, currNodeTimeTaken + node.time);
38 | });
39 |
40 | return;
41 | }
42 |
43 |
44 |
45 | /**
46 | * Follow up question of above
47 | */
48 |
49 | function orderOfNodesGettingWet(root) {
50 | const heap = new minHeap();
51 | heap.add(root);
52 | while(!heap.empty()) {
53 | const minTimeNode = heap.extractMin();
54 |
55 | console.log(minTimeNode);
56 |
57 | minTimeNode.childrens.forEach((node) => {
58 | node.time += minTimeNode.time;
59 | heap.add(node);
60 | });
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/reanpx/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/reanpx/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35 |
36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39 |
40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
--------------------------------------------------------------------------------
/reanpx/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Hall of Fame
2 | [](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/0)[](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/1)[](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/2)[](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/3)[](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/4)[](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/5)[](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/6)[](https://sourcerer.io/fame/rahulrana95/rahulrana95/Interview_Questions_Data_Structures_And_Algo_Frontend_Engineer/links/7)
3 |
4 |
5 | This repo is about the questions asked in interviews specially in frontend engineer domain. It has mentioned which questions covered which concept so its easy to find concept wise , company wise questions and level wise questions.
6 |
7 | ..
8 | Contribution Guidelines:
9 | 1. Try to write a production level meaningful code.
10 | 2. Cover all edge cases
11 | 3. At top make a template like this
12 | /**
13 | * Original questions link for clear problem statement or someone might want to try code there
14 | * Level: Easy, Medium, Hard
15 | * Tags: Company this question asked in, DS Involved, Algo involved, Famous concept used
16 | * Avg time taken:
17 | * Concept covered
18 | */
19 |
20 | Explain arguments of fucntion with jsdoc style comments
21 |
22 |
23 | Please checkout a branch from master with name question/ and open a pr to master
24 | File name rules: Q-_>.js or any format you want
--------------------------------------------------------------------------------
/reanpx/src/components/progress-bars/workingcode.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 |
3 | const Bar = ({ progress, index }) => {
4 | return
5 | {index + 1}
6 |
7 | progress: {progress}
8 |
9 | }
10 |
11 | const CONCURRENCY_BARS = 3;
12 |
13 |
14 | export default function App() {
15 | const [bars, setBars] = React.useState({});
16 | let idNum = React.useRef(0);
17 | let idsREf = React.useRef(new Array());
18 |
19 | const [progress, setProgress] = React.useState({});
20 | const [barQueue, setBarqueue] = React.useState([]);
21 | const [start, setStart] = React.useState('start');
22 | const [numBarInProgress, setNumBarInProgress] = React.useState(0);
23 |
24 | React.useEffect(() => {
25 | let intervalId;
26 |
27 | if (start === 'pause') {
28 | idsREf.current.forEach((timeID) => {
29 | clearInterval(timeID);
30 | })
31 | return;
32 | }
33 |
34 | if (barQueue.length <=0 || numBarInProgress>=6) {
35 |
36 | return;
37 | }
38 |
39 | const newBarQueue = [...barQueue];
40 | const bar = newBarQueue.shift();
41 | setBarqueue(newBarQueue);
42 | console.log('number of bars running is ', numBarInProgress)
43 |
44 | const delay = 100;
45 | setNumBarInProgress((numBarInProgress) => numBarInProgress+2);
46 | intervalId = setInterval(() => {
47 | setBars((bars) => {
48 | let currentProgress = Math.floor(bars[bar.id].progress + (delay/2000)*100);
49 |
50 | if (currentProgress > 100) {
51 | currentProgress = 100;
52 | clearInterval(intervalId);
53 | console.log('task completed ', bar.id);
54 | setNumBarInProgress((numBarInProgress) => numBarInProgress-1);
55 | }
56 | return {
57 | ...bars,
58 | [bar.id]: {
59 | ...bars[bar.id],
60 | progress: currentProgress,
61 | lastTime:new Date().getTime()
62 | }
63 | }
64 | })
65 | },delay)
66 |
67 | idsREf.current.push(intervalId);
68 | console.log('ids.current is ',idsREf.current);
69 |
70 |
71 | }, [barQueue,numBarInProgress, start])
72 |
73 | const onAddClick = () => {
74 | const id = idNum.current;
75 | idNum.current = idNum.current + 1;
76 |
77 | const obj = {
78 | id,
79 | progress: 0,
80 | startTime: new Date().getTime(),
81 | lastTime:new Date().getTime()
82 | };
83 |
84 | setBars((bars) => {
85 |
86 | return {
87 | ...bars,
88 | [id]: obj
89 | }
90 | })
91 |
92 | setBarqueue((barQueue) => {
93 | return [...barQueue, obj];
94 | })
95 | }
96 |
97 | const onStartClick = () => {
98 | if (start === 'start') {
99 | setStart('pause');
100 | } else {
101 | setStart('start');
102 | setNumBarInProgress(() => 0);
103 | setBarqueue((barQueue) => {
104 | const ans = [];
105 | console.log('bars is ',bars);
106 | Object.values(bars).forEach((bar) => {
107 | if (bar.progress > 0 && bar.progress< 100) {
108 | ans.push({...bar, lastTime: new Date().getTime()})
109 | }
110 | })
111 | return [...ans,...barQueue]
112 | })
113 |
114 | }
115 | }
116 |
117 | const onResetClick = () => {
118 |
119 | }
120 |
121 |
122 |
123 | return (
124 |
125 |
126 |
127 |
128 |
129 | {Object.keys(bars).map((barKey, index) => )}
130 |
131 |
132 | );
133 | }
134 |
--------------------------------------------------------------------------------
/reanpx/src/components/progress-bars/index.tsx:
--------------------------------------------------------------------------------
1 | // @ts-nocheck
2 | import React from 'react';
3 |
4 | type BarPropsT = {
5 | progress: number,
6 | index: number
7 | }
8 |
9 | type BarType = {
10 | id: number,
11 | progress: number
12 | }
13 |
14 | const Bar = ({ progress, index }: BarPropsT) => {
15 | return
16 | {index + 1}
17 |
18 | progress: {progress}
19 |
20 | }
21 |
22 | const CONCURRENCY_BARS: number = 3;
23 |
24 |
25 | type BarStateTypeVal = {
26 | string: BarType
27 | } | null
28 |
29 | type BarsStateT = BarStateTypeVal | ((bars: { string: BarType; } | null) => { string: BarType })
30 |
31 | export default function ProgressBars() {
32 | const [bars, setBars] = React.useState(null);
33 | let idNum = React.useRef(0);
34 | let idsREf = React.useRef(new Array());
35 |
36 | const [barQueue, setBarqueue] = React.useState([]);
37 | const [start, setStart] = React.useState<'start' | 'pause'>('start');
38 | const [numBarInProgress, setNumBarInProgress] = React.useState(0);
39 |
40 | React.useEffect(() => {
41 | let intervalId:ReturnType;
42 |
43 | if (start === 'pause') {
44 | idsREf.current.forEach((timeID) => {
45 | clearInterval(timeID);
46 | })
47 | return;
48 | }
49 |
50 | if (barQueue.length <=0 || numBarInProgress>=6) {
51 |
52 | return;
53 | }
54 |
55 | const newBarQueue: BarType[] = [...barQueue];
56 | const bar: BarType | undefined = newBarQueue.shift();
57 | setBarqueue(newBarQueue);
58 | console.log('number of bars running is ', numBarInProgress)
59 |
60 | const delay = 100;
61 | setNumBarInProgress((numBarInProgress) => numBarInProgress+2);
62 | intervalId = setInterval(() => {
63 | setBars((bars: {string: BarType} | null) => {
64 | let currentProgress = bar && bars && Math.floor(bars[String(bar.id)]?.progress + (delay/2000)*100);
65 |
66 | if (currentProgress && currentProgress > 100) {
67 | currentProgress = 100;
68 | clearInterval(intervalId);
69 | console.log('task completed ', bar?.id);
70 | setNumBarInProgress((numBarInProgress) => numBarInProgress-1);
71 | }
72 | return {
73 | ...bars,
74 | [bar?.id ?? '']: {
75 | ...bars[bar?.id ?? ''],
76 | progress: currentProgress,
77 | lastTime:new Date().getTime()
78 | }
79 | }
80 | })
81 | },delay)
82 |
83 | idsREf.current.push(intervalId);
84 | console.log('ids.current is ',idsREf.current);
85 |
86 |
87 | }, [barQueue,numBarInProgress, start])
88 |
89 | const onAddClick = () => {
90 | const id = idNum.current;
91 | idNum.current = idNum.current + 1;
92 |
93 | const obj = {
94 | id,
95 | progress: 0
96 | };
97 |
98 | setBars((bars) => {
99 |
100 | return {
101 | ...bars,
102 | [id]: obj
103 | }
104 | })
105 |
106 | setBarqueue((barQueue) => {
107 | return [...barQueue, obj];
108 | })
109 | }
110 |
111 | const onStartClick = () => {
112 | if (start === 'start') {
113 | setStart('pause');
114 | } else {
115 | setStart('start');
116 | setNumBarInProgress(() => 0);
117 | setBarqueue((barQueue) => {
118 | const ans:BarType[] = [];
119 |
120 | Object.values(bars).forEach((bar) => {
121 | if (bar.progress > 0 && bar.progress< 100) {
122 | ans.push({...bar})
123 | }
124 | })
125 | return [...ans,...barQueue]
126 | })
127 |
128 | }
129 | }
130 |
131 | const onResetClick = () => {
132 |
133 | }
134 |
135 |
136 |
137 | return (
138 |
139 |
140 |
141 |
142 |
143 | {Object.keys(bars || {}).map((barKey, index) => )}
144 |
145 |
146 | );
147 | }
148 |
--------------------------------------------------------------------------------