If you're looking to tackle multiple math questions simultaneously, such as 5+6+7+8-6-1*5+1
=16
, 5+6+7+8-6-1*(5+1)
=14
, and 5**5
=3125
(which is equivalent to 5*5*5*5*5
=3125
), then this code could be the solution.
Note: Apologies if my explanation is hard to follow. English isn't my strong suit.
Using eval() can be risky but results in lightweight code. To ensure safety, add if (!(/[a-z]/i.test(Equation))
to filter out equations that contain other code (e.g., 1+'alert("You get infect")'+5) and only accept equations with numbers and basic operators (e.g., 1+5+9*8/5-9).
var Equation = prompt("Equation? (Can only take 1 math equation for now. Upcoming updates soon!)");
if (Equation === null) close();
if (!(/[a-z]/i.test(Equation)) && Equation.match(/\+|\-|\/|\*/g)) {
var Answer = eval(Equation);
alert(Answer);
console.log(Answer)
var CO = confirm("Equation Done, Goodbye!");
} else {alert(`Equation contain other words`)}
if (CO === true) {
close();
}
if (CO === false) {
close();
}
This script functions as a straightforward calculator.
var screen = document.querySelector(".screen > span");
document.querySelector(".equal-item").onclick = () => calc();
document.querySelector(".clear").onclick = () => screen.textContent = '';
document.querySelector(".back").onclick = () => screen.textContent = screen.textContent.substring(0, screen.textContent.length - 1);
document.querySelectorAll(".touche__box-item").forEach(a => a.onclick = () => screen.textContent += a.id)
const calc = () => {
var Equation = screen.textContent;
if (Equation === null) return;
if (Equation.match(/\+|\-|\/|\*/g)) {
var additionAwnser = eval(Equation);
screen.textContent = additionAwnser;
}
}
.calculator {
width: 408px;
height: 837px;
margin: 10px auto;
position: relative;
color: #000;
font-size: 3em;
}
/* CSS styling information continued here... */
<html lang="en">
<head>
</head>
<body>
<main>
<div class="container calculator">
<div class="screen-item screen"> <span></span></div>
<dl class="touche__box">
<dt class="clear-item clear"> <span>C</span></dt><dt class="clear-item back"> <span><</span></dt>
<dt class="touche__box-item" id="-"> <span class="soustraction">_</span></dt>
<dt class="touche__box-item" id="+"><span class="sign">+</span></dt>
<!-- Additional HTML structure for the calculator buttons -->
</dl>
</div>
</main>
</body>
</html>