In comp.lang.javascript message <733f879c-e602-46dd-98aa-226d82210f4c@y3
8g2000hsy.googlegroups.com>, Fri, 16 May 2008 02:31:12, Michal
<re****@gmail.composted:
>For example:
var a=8;
var b=6;
function c(a,b) {
return a*b;
}
And now I want that "c" will return me 56 and I want to do that only
by changing "b" and this "goal seek" function will return me solution
that "b" must be equal 7
Go to <URL:http://www.merlyn.demon.co.uk/js-demos.htm#FZ>, insert
in the textarea
function c(a,b) { return a*b; }
c(8, X) - 56
put Min X =, say, 0 and Max X = 50, press Find, and read the answer.
Then read the whole page, and View Source to find out how it is done :
it uses
function FindZero(F) { with (F) {
var j, X, XV, HV, LV, Fn = EX1.value
var Lo = userIn(Min1), Hi = userIn(Max1)
X = Lo ; LV = eval(Fn)
X = Hi ; HV = eval(Fn)
if ((LV*HV)>=0) { Ans1.value="Bad bounds" ; return }
for (j=0; j<40; j++) {
X = (Hi+Lo)/2 ; XV = eval(Fn) ; if (XV==0) break
if ((XV*LV)<0) { Hi = X ; HV = XV } else { Lo = X ; LV = XV }
}
Ans1.value="X = "+SigFigExp((Hi+Lo)/2, 8) } }
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.