Jump to content

can i use variable inside str.replace() ?


alzami
 Share

Recommended Posts

i am trying a dynamic max power and "x" value input.everything is fine the problem is i can not replace the (x^something) dynamically with replace() method.

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title><style>#gap{    width:200px;    height:50px;    border:1px solid black;}thiss{    width:200px;    height:50px;}</style></head><body><div id="gap"></div><p id="thiss"></p><input type="button" value="replace this" onClick="change();"><script>function change(){    var m=document.getElementById("gap");    var n=document.getElementById("thiss");    var t=document.getElementById("thiss").innerHTML;    var power=prompt("input highest power of x:");    var i;    var arr=new Array();    for( i=power;i>=0;i--){        arr[i]=prompt("coeff of x^"+i);    }    i=power;    for(i=power;i>=0;i--){        if(arr[i]>0 && i==power){            n.innerHTML += arr[i]+"*x^"+i;        }        else if(arr[i]<0 && i==power){            n.innerHTML += arr[i]+"*x^"+i;        }        else if(arr[i]>0 && i!=power){    n.innerHTML += "+"+arr[i]+"*x^"+i;        }        else if(arr[i]<0){            n.innerHTML += arr[i]+"*x^"+i;        }else if(arr[i]==0){            n.innerHTML += "";        }                }    var x_val=prompt("solve with x =");i=power;for(i=power;i>=0;i--){    m.innerHTML=eval(t.replace(/x^i/,"Math.pow(x_val,i)"));}    }</script></body></html>

mainly problem is here.i know this is garbage :(.but how can it be fixed.how can i replace using variable

for(i=power;i>=0;i--){	m.innerHTML=eval(t.replace(/x^i/,"Math.pow(x_val,i)"));}
Edited by alzami
Link to comment
Share on other sites

eval() runs code in the window scope, I believe, so x_val and i are undefined. You can solve it by putting the value itself into the string:

"Math.pow(" + x_val + "," + i + ")"

I don't know about all this string manipulation and evaluation, though. I think there might be a better way to solve this, like breaking the string into a few pieces and doing different operations on each piece.

Link to comment
Share on other sites

I remember one time of using trees in a college class (so that one could use prefix,infix,and postfix notation). if you build the tree as nodes (which will be the operator) and leaves (the operands).

 

for example given the formula 2+5*6-13 the root node will spit the formula at it's lowest order of operation ( the plus and minus have the lowest order, but the minus comes last so it's the lowest). so the root node is split with the leaves of "2+5*6" and "13" with the operator "-" connecting them. we then look at the left leaf thats not fullt split out yet and do the same thing. so that formula is split as "2"on the left, "+" as the operator, and "5*6" on the right. now that the "+" node is split it checks the left to see if it's fully split (and "2" is fully split), then checks the right to see if "5*6" is split, its not so it splits that.

 

In the end the tree it builds looks something like this, and in solving it, it'll take these steps working bottom up:

       (-)                (-)               (-)      -->   (19)       /                 /                /      (+) (13)           (+) (13)   -->   (32) (13)     /                 /    (2) (*)     -->    (2) (30)       /      (5) (6)

you can then make recursive calls to calculate the overall value of the formula. +,-, and * are simple operators to code for... exponents, while needing a little more code, is also still pretty simple.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...