Skip to content

Commit

Permalink
Refactor code and add utils module folder and files to clean up the c…
Browse files Browse the repository at this point in the history
…ode base.
  • Loading branch information
tsheporamantso committed Nov 8, 2024
1 parent 14f45f2 commit 3cc0c24
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"singleQuote": true
}
}
9 changes: 9 additions & 0 deletions numbers/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/extensions */
import getElement from './utils/getElement.js';
import updateCount from './utils/updateCount.js';

const numbers = [...getElement('.number')];

numbers.forEach((number) => {
updateCount(number);
});
13 changes: 13 additions & 0 deletions numbers/utils/getElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable comma-dangle */
const getElement = (selection) => {
const element = document.querySelectorAll(selection);

if (element) {
return element;
}
throw new Error(
`Please verify selected ("${selection}"), no such element exist!`
);
};

export default getElement;
18 changes: 18 additions & 0 deletions numbers/utils/updateCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const updateCount = (el) => {
const value = +el.dataset.value;
const increment = Math.ceil(value / 1000);
let initialValue = 0;

const increaseCount = setInterval(() => {
initialValue += increment;

if (initialValue > value) {
el.textContent = `${value}+`;
clearInterval(increaseCount);
return;
}
el.textContent = `${initialValue}+`;
}, 1);
};

export default updateCount;

0 comments on commit 3cc0c24

Please sign in to comment.