By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

Simplyfiying fractions in Javascript

P: n/a
Does anybody have a script to simplify fractions?

Jan 13 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Or just like a recommendation?

Jan 13 '06 #2

P: n/a

mo********@gmail.com wrote:
Or just like a recommendation?

What exactly do you need? Please provide sample input/output...

Jan 13 '06 #3

P: n/a
mo********@gmail.com wrote:
Does anybody have a script to simplify fractions?


To simplify a fraction, find the greatest common divisor of the numerator
and the denominator of the fraction and divide both by it if there is one
greater than 1.

<URL:http://google.com/search?q=simplify+fraction&filter=0>
HTH

PointedEars
Jan 13 '06 #4

P: n/a
JRS: In article <11**********************@g14g2000cwa.googlegroups .com>
, dated Thu, 12 Jan 2006 17:52:27 local, seen in
news:comp.lang.javascript, mo********@gmail.com <mo********@gmail.com>
posted :
Does anybody have a script to simplify fractions?


Let the fraction be X/Y, X & Y being variables.

function HCF(u, v) { var U = u, V = v
while (true) {
if (!(U%=V)) return V
if (!(V%=U)) return U } }

T = HCF(X, Y) ; X /= T ; Y /= T ;

HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
only with positive integer parameters, but full compliance is not
obligatory.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 13 '06 #5

P: n/a
Dr John Stockton said the following on 1/13/2006 3:03 PM:
JRS: In article <11**********************@g14g2000cwa.googlegroups .com>
, dated Thu, 12 Jan 2006 17:52:27 local, seen in
news:comp.lang.javascript, mo********@gmail.com <mo********@gmail.com>
posted :
Does anybody have a script to simplify fractions?

Let the fraction be X/Y, X & Y being variables.

function HCF(u, v) { var U = u, V = v


Just for my own curiosity, does the U = u and V = v serve any useful
purpose? Meaning, does the code fail if you use u and v below instead of
U and V?

It shouldn't is why I am asking.
while (true) {
if (!(U%=V)) return V
if (!(V%=U)) return U } }

T = HCF(X, Y) ; X /= T ; Y /= T ;

HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
only with positive integer parameters, but full compliance is not
obligatory.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 14 '06 #6

P: n/a
Randy Webb <Hi************@aol.com> writes:
Dr John Stockton said the following on 1/13/2006 3:03 PM:

function HCF(u, v) { var U = u, V = v


Just for my own curiosity, does the U = u and V = v serve any useful
purpose? Meaning, does the code fail if you use u and v below instead
of U and V?


Not in this case. Both "u" and "U" are local variables, and since "u"
is never used again, there is no immediate need to rename it.

I'm guessing it's a general strategy to avoid changing the parameters,
in case they are needed again later.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 14 '06 #7

P: n/a
thanks all

Jan 14 '06 #8

P: n/a
JRS: In article <W6********************@comcast.com>, dated Fri, 13 Jan
2006 21:38:20 local, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Dr John Stockton said the following on 1/13/2006 3:03 PM:

Let the fraction be X/Y, X & Y being variables.

function HCF(u, v) { var U = u, V = v


Just for my own curiosity, does the U = u and V = v serve any useful
purpose? Meaning, does the code fail if you use u and v below instead of
U and V?

It shouldn't is why I am asking.
while (true) {
if (!(U%=V)) return V
if (!(V%=U)) return U } }

T = HCF(X, Y) ; X /= T ; Y /= T ;

HCF is used in js-dates.htm, via FAQ, see below. It is intended for use
only with positive integer parameters, but full compliance is not
obligatory.


Usually at least, ISTM that it will not fail. I converted it from
another language, in which there are three possible ways of passing
parameters and the one which copies must be used (another won't compile
with that body; the third will change the external quantities). I put
the copying in as being easier than deciding whether it might ever
matter.

The only javascript in which I at present use it is

F = A[0] ; J = N ; while (J--) F = HCF(F, A[J])

after which A is abandoned and F is displayed - so I'd not notice the
difference anyway even if there were any.

Code, in js-dates.htm, changed. Thanks.

<URL:http://www.merlyn.demon.co.uk/hcfactor.pas> has several HCF
algorithms.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Jan 15 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.