How can you make a Calculator?
How can you make a Calculator in HTML, using JS Libraries and the DOM, but NOT USING PJS?
Hi,
This is not a question about site help... I encourage you to try it out or google something to find an easy answer. Go below any project of yours and ask in the "help requests" section, after you have a good foundation people will help you out.
I think this is not a place to ask a question like that but here are some of the ways that can help you.
You can create a calculator in HTML using JavaScript and the DOM (Document Object Model) without using any additional JavaScript libraries such as PJs. Here is an example of how you can create a simple calculator:
<div id="calculator">
<input type="text" id="display" disabled>
<br>
<button>7</button>
<button>8</button>
<button>9</button>
<button class="operator">+</button>
<br>
<button>4</button>
<button>5</button>
<button>6</button>
<button class="operator">-</button>
<br>
<button>1</button>
<button>2</button>
<button>3</button>
<button class="operator">*</button>
<br>
<button>0</button>
<button class="operator">/</button>
<button id="equal">=</button>
<button id="clear">C</button>
</div>
const buttons = document.querySelectorAll('button');
const display = document.querySelector('#display');
buttons.forEach(button => {
button.addEventListener('click', e => {
const value = e.target.innerText;
if (!isNaN(value)) {
display.value += value;
} else if (value === 'C') {
display.value = '';
} else if (value === '=') {
display.value = eval(display.value);
} else {
display.value += value;
}
});
});
const equal = document.querySelector('#equal');
equal.addEventListener('click', e => {
display.value = eval(display.value);
});
This is a basic example of how you can create a calculator using HTML, JavaScript, and the DOM, without using any additional libraries. You can style the calculator using CSS and add more features like history or memory.
I hope this helps! Next time try to use another website, & let me know if you have any other questions.
Is "eval" harmful?
Eval is not recommended for real-world programming, but if you’re careful with it, then it’s fine.
Please sign in to leave a comment.