Question:
JavaScript: How to change a string into a valid expression?
Daniel N
2011-06-13 00:47:09 UTC
I'm working on a scientific JavaScript-based calculator.. I've been having a little trouble, so I'm trying a new method where the expression (Let's say 3+3) is a string inside an input div.. Then when somebody presses the "=" button, I want JavaScript to compute it. But the problem I'm having is getting JavaScript to evaluate a string as an expression. How can I convert the string into a mathematical expression?
Here's the code:
function compute(){
var input = document.getElementById("input");
var output = document.getElementById("output");
output.innerHTML = input.innerHTML;
}
So let's say I have 3+3 in the input div.. And I want 6 to pop out in the output div. How do I change the function so that 3+3, the string, is computed into 6?
Three answers:
AnalProgrammer
2011-06-13 00:49:34 UTC
Have fun.
?
2017-01-20 09:49:04 UTC
probable in simple terms to slot in as properly. final 3 hundred and sixty 5 days while the emo fad hit my city, an identical factor occurred. The chicks razored off their hair and began to placed on black, and the dudes began to placed on those abominations referred to as skinny denims and characteristic a edge of hair hide their eye. not something in my view against emos, yet I hate the so referred to as "emo" track. i've got alot of them sprouted strolling around with Underoath and Paramore shirts. i understand those are not particularly emo bands yet I quite tend to work out those on emo young infants. PLUS, emo is handy while warm subject rely is suited around the corner!
anonymous
2011-06-13 01:46:05 UTC
//Get input value

var input = document.getElementById("input").value;



//Get DIV tag object

var output = document.getElementById("output");



//Assign out value

output.innerHTML = parseInt(input ) + parseInt(input );


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...