View Single Post
Old 12-12-2012, 09:43 PM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
if you wanted to avoid the much-despised eval...

Code:
var str=function(s){
return eval(s.replace(/x/gi,"*"))
}

alert(str("14X10+2X1"))
you could make a dynamic script...

Code:
var str = function (s){
  var scr = document.createElement("script");
  scr.type = "text/javascript";
  scr.text = "ans = " + s.replace(/x/gi,"*") + ";";
  document.body.appendChild(scr);
  document.body.removeChild(scr);
  return ans;
}

alert(str("14X10+2X1"))
although I suspect they amount to much the same thing...
xelawho is offline   Reply With Quote