/    Sign up×
Community /Pin to ProfileBookmark

Reducing fraction

Hello everyone,
I have a function that is suppose to reduce the fraction. Unfortunately it is not reducing. My script is a kids math program that will have the user add, subtract, mutliply and divide fractions. Right now I am calling the function only in the mutiplication portion of the script. Can someone tell me where I am going wrong and what I might do to correct it?

The function:

[code]
function reduce(numerator,denominator) {
var gcd = function gcd(a,b) {
return b ? gcd(b, a%b) : a;
};
gcd = gcd(numerator,denominator);
return [numerator/gcd, denominator/gcd];
}
[/code]

Where I am using it in the code:

[code]
function multiply() {
if(document.quizform.arithmetic[0].checked)
maxValue=10;
else {
if(document.quizform.arithmetic[1].checked)
maxValue=30;
else {
maxValue=60
}
}

n1=ranom(maxValue);
d1=random(maxValue);
n2=ranom(maxValue);
d2=random(maxValue);
An=(n1 * n2);
Ad=(d1 * d2);
reduce(An, Ad); <—————Here
numAns=(An + “/” + Ad);
Answer=window.prompt( n1 + “/” + d1 + ” * ” + n2 + “/” + d2 + ” = “, 0);
ans();
}
[/code]

If you need more of the program let me know.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Apr 14.2012 — n1=ran[B][COLOR="Red"]d[/COLOR][/B]om(maxValue);There probably. And, furthermore, unless you've defined random yourself, you're looking for Math.floor(Math.random()*maxValue);
Copy linkTweet thisAlerts:
@nathanwallApr 14.2012 — "random" is misspelled. That might be why. I tested the reduce function; it seems to be working in the limited tests I tried. If it's not working for specific numbers, which ones?

<i>
</i>function multiply() {
if(document.quizform.arithmetic[0].checked)
maxValue=10;
else {
if(document.quizform.arithmetic[1].checked)
maxValue=30;
else {
maxValue=60
}
}

[COLOR="Red"]n1=ranom(maxValue); // &lt;-------- misspelled[/COLOR]
d1=random(maxValue);
[COLOR="Red"]n2=ranom(maxValue); // &lt;-------- misspelled[/COLOR]
d2=random(maxValue);
An=(n1 * n2);
Ad=(d1 * d2);
reduce(An, Ad);
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " * " + n2 + "/" + d2 + " = ", 0);
ans();
}
Copy linkTweet thisAlerts:
@csharp100authorApr 15.2012 — n1=ran[B][COLOR="Red"]d[/COLOR][/B]om(maxValue);There probably. And, furthermore, unless you've defined random yourself, you're looking for Math.floor(Math.random()*maxValue);[/QUOTE]

I did define random myself.
Copy linkTweet thisAlerts:
@csharp100authorApr 15.2012 — "random" is misspelled. That might be why. I tested the reduce function; it seems to be working in the limited tests I tried. If it's not working for specific numbers, which ones?

<i>
</i>function multiply() {
if(document.quizform.arithmetic[0].checked)
maxValue=10;
else {
if(document.quizform.arithmetic[1].checked)
maxValue=30;
else {
maxValue=60
}
}

[COLOR="Red"]n1=ranom(maxValue); // &lt;-------- misspelled[/COLOR]
d1=random(maxValue);
[COLOR="Red"]n2=ranom(maxValue); // &lt;-------- misspelled[/COLOR]
d2=random(maxValue);
An=(n1 * n2);
Ad=(d1 * d2);
reduce(An, Ad);
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " * " + n2 + "/" + d2 + " = ", 0);
ans();
}
[/QUOTE]


The mispelling is not the problem either. These are just names for function calls. Ranom is for the numerator and Random is for the denominator. Where I have the function call for reduce(An, Ad) will or should show up in the answer box. What the program does is pops up a box with two fractions to say... add, and asks the user to add the two fractions and put in the answer. If the answer is incorrect it shows the correct answer. I will post the code for alll to try. I might have it misplaced but do not think I do.
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;&lt;head&gt;

&lt;script language="JavaScript"&gt;

&lt;!-- Begin
correct=0;
wrong=0;

//The following functions are used to generate different numerators and denominators

function random(maxValue) {
day= new Date();
hour= day.getHours();
min=day.getMinutes();
sec=day.getSeconds();
mili=day.getTime()
return(((hour*1011)+(min*17)+(sec)+mili) &amp;#37; maxValue);
}

function ranom(maxValue) {
day= new Date();
mil=day.getTime();
return((mil) % maxValue);
}

function random1(maxValue) {
day= new Date();
hour= day.getHours();
min=day.getMinutes();
sec=day.getSeconds();
mili=day.getTime()
return(((hour*3618)+(min*67)+(sec)+mili) % maxValue);
}

function ranom1(maxValue) {
day= new Date();
mil=day.getTime();
return((mil*18) % maxValue);
}

function add() {
if(document.quizform.arithmetic[0].checked)
maxValue=6;
else {
if(document.quizform.arithmetic[1].checked)
maxValue=10;
else {
maxValue=20;
}
}

n1=ranom(maxValue);
d1=random(maxValue);
n2=ranom1(maxValue);
d2=random1(maxValue);
An=((n1*d2) + (n2*d1));
Ad=(d1*d2)
$g = gcd(An, Ad);
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " + " + n2 + "/" + d2 + " = ", "");
ans();
}

function subtract() {
if(document.quizform.arithmetic[0].checked)
maxValue=10;
else{
if(document.quizform.arithmetic[1].checked)
maxValue=30;
else {
maxValue=60
}
}

n1=ranom(maxValue);
d1=random(maxValue);
n2=ranom(maxValue);
d2=random(maxValue);
An=((n1*d2) - (n2*d1));
Ad=(d1*d2);
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " - " + n2 + "/" + d2 + " = ", 0);
ans()
}

function divide() {
if(document.quizform.arithmetic[0].checked)
maxValue=10;
else {
if(document.quizform.arithmetic[1].checked)
maxValue=30;
else {
maxValue=60
}
}

n1=ranom(maxValue);
d1=random(maxValue);
n2=ranom(maxValue);
d2=random(maxValue);
An=(n1 * d2);
Ad=(d1 * n2);
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " / " + n2 + "/" + d2 + " = ", 0);
ans()
}

function multiply() {
if(document.quizform.arithmetic[0].checked)
maxValue=10;
else {
if(document.quizform.arithmetic[1].checked)
maxValue=30;
else {
maxValue=60
}
}

n1=ranom(maxValue);
d1=random(maxValue);
n2=ranom(maxValue);
d2=random(maxValue);
An=(n1 * n2);
Ad=(d1 * d2);
reduce(An, Ad); //&lt;----------------------------------------------------------------------------------Here
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " * " + n2 + "/" + d2 + " = ", 0);
ans();
}

function reduce(numerator,denominator) {
var gcd = function gcd(a,b) {
return b ? gcd(b, a%b) : a;
};
gcd = gcd(numerator,denominator);
return [numerator/gcd, denominator/gcd];
}

function check() {
if ((correct+wrong) != 0) {
score = "" + ((correct / (correct + wrong)) * 100);
score = score.substring(0,4) + "%";
alert("YOUR SCORE: " + score + "n"
+ correct + " correctn"
+ wrong + " incorrect")
}
else alert("You have not completed any exercises yet.");
}

function ans() {
if (Answer == numAns) {
correct++;
msg = "Congratulations, your answer is correct.";
}
else {
wrong++;
msg = "Oops! " + Answer + " is incorrect.nn"
+ "The correct answer was " +numAns + ".";
}

<i> </i>score = "" + ((correct / (correct + wrong)) * 100);
<i> </i>score = score.substring(0,4) + "%";
<i> </i>alert(msg + "nnYOUR SCORE: " + score + "n"
<i> </i>+ correct + " correctn"
<i> </i>+ wrong + " incorrect")
}

// End --&gt;
&lt;/script&gt;
&lt;title&gt;Clint's kidsmath.p2&lt;/title&gt;


&lt;/head&gt;&lt;body&gt;
&lt;center&gt;
&lt;form name="quizform"&gt;&lt;input value="add" onclick="add()" type="button"&gt;
&lt;input value="subtract" onclick="subtract()" type="button"&gt;&lt;input value="multiply" onclick="multiply()" type="button"&gt; &lt;input value="divide" onclick="divide()" type="button"&gt;&lt;br&gt;
&lt;br&gt;
&lt;input name="arithmetic" type="radio"&gt;Easy &lt;input name="arithmetic" checked="checked" type="radio"&gt;Moderate &lt;input name="arithmetic" type="radio"&gt;Difficult &lt;br&gt;
&lt;br&gt;
Do not simpify your answer!&lt;br&gt;
&lt;br&gt;
&lt;input value="Check Score" onclick="check()" type="button"&gt; &lt;input value="Reset Score" onclick="javascript:correct=0;wrong=0;" type="button"&gt;&lt;/form&gt;
&lt;/center&gt;

&lt;/body&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@csharp100authorApr 15.2012 — Ok, I found out my problem. The reduce(An, Ad) is in the wrong place. Nothing was actually calling the function. Now my question is, can this function return a formatted print?
<i>
</i>function reduce(numerator,denominator) {
var gcd = function gcd(a,b) {
return b ? gcd(b, a&amp;#37;b) : a;
};
gcd = gcd(numerator,denominator);
return [numerator/gcd, denominator/gcd];
}


My function call is now:
<i>
</i>numAns=reduce(An, Ad);

My other question is, is there a way to make sure my ranon and random functions do not return a 0 (zero)? I seem to get quite a few 0's in the numerator.
Copy linkTweet thisAlerts:
@csharp100authorApr 15.2012 — Ok got the print format, I just changed this line
<i>
</i>return [numerator/gcd, denominator/gcd];

to
<i>
</i>return [numerator/gcd + "/" + denominator/gcd];
Copy linkTweet thisAlerts:
@WyCnetApr 16.2012 — What value does maxValue have if multiply() is not invoked?
Copy linkTweet thisAlerts:
@csharp100authorApr 17.2012 — What value does maxValue have if multiply() is not invoked?[/QUOTE]

Up to 6 or 10 or 20 depending on whether easy, medium or hard is chosen. My question is and I know I am spacing on this, how can I get a greater than 0 and less than 7 in this line:
<i>
</i>maxValue=6;
Copy linkTweet thisAlerts:
@Declan1991Apr 17.2012 — Math.floor(Math.random()*maxValue)+lowestvalue;

That'll give you a pseudorandom number, n, lowestvalue <= n < maxValue.
return [numerator/gcd + "/" + denominator/gcd];Pointless to put it in an array, if you want it to return a string, return a string:return numberator/gcd + "/" + denominator/gcd;
×

Success!

Help @csharp100 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...