473,721 Members | 2,295 Online

# Simplyfiying fractions in Javascript

Does anybody have a script to simplify fractions?

Jan 13 '06 #1
8 4550
Or just like a recommendation?

Jan 13 '06 #2

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

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

Jan 13 '06 #3
mo********@gmai l.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=simpli fy+fraction&fil ter=0>
HTH

PointedEars
Jan 13 '06 #4
JRS: In article <11************ **********@g14g 2000cwa.googleg roups.com>
, dated Thu, 12 Jan 2006 17:52:27 local, seen in
news:comp.lang. javascript, mo********@gmai l.com <mo********@gma il.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 13 '06 #5
Dr John Stockton said the following on 1/13/2006 3:03 PM:
JRS: In article <11************ **********@g14g 2000cwa.googleg roups.com>
, dated Thu, 12 Jan 2006 17:52:27 local, seen in
news:comp.lang. javascript, mo********@gmai l.com <mo********@gma il.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 14 '06 #6
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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 14 '06 #7
thanks all

Jan 14 '06 #8
JRS: In article <W6************ ********@comcas t.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.demo n.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.demo n.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Jan 15 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 33 6624 by: selowitch | last post by: I've been searching in vain for a way to present typographically correct fractions (not resorting to and tags) but have been frustrated by the fact that the glyphs for one-half, one-eighth, three-quarters, etc. do not display in the correct typeface (or even consistently the same typeface) and seem totally resistant to attempt to fix this through CSS: ". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in... 0 9077 by: tracyyun | last post by: Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some... 0 8019 by: agi2029 | last post by: Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea... 1 6676 by: isladogs | last post by: The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will... 0 5992 by: conductexam | last post by: I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();... 0 4761 by: adsilva | last post by: A Windows Forms form does not have the event Unload, like VB6. What one acts like? 3 2137 by: bsmnconsultancy | last post by: In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.