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];
}
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();
}
If you need more of the program let me know.
Code:
n1=rand om(maxValue);
There probably. And, furthermore, unless you've defined random yourself, you're looking for
Code:
Math.floor(Math.random()*maxValue);
Great wit and madness are near allied, and fine a line their bounds divide.
"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?
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); // <-------- misspelled
d1=random(maxValue);
n2=ranom(maxValue); // <-------- misspelled
d2=random(maxValue);
An=(n1 * n2);
Ad=(d1 * d2);
reduce(An, Ad);
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " * " + n2 + "/" + d2 + " = ", 0);
ans();
}
Originally Posted by
Declan1991
Code:
n1=rand om(maxValue);
There probably. And, furthermore, unless you've defined random yourself, you're looking for
Code:
Math.floor(Math.random()*maxValue);
I did define random myself.
Originally Posted by
nathanwall
"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?
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); // <-------- misspelled
d1=random(maxValue);
n2=ranom(maxValue); // <-------- misspelled
d2=random(maxValue);
An=(n1 * n2);
Ad=(d1 * d2);
reduce(An, Ad);
numAns=(An + "/" + Ad);
Answer=window.prompt( n1 + "/" + d1 + " * " + n2 + "/" + d2 + " = ", 0);
ans();
}
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.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<script language="JavaScript">
<!-- 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) % 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); //<----------------------------------------------------------------------------------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 + " correct\n"
+ 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.\n\n"
+ "The correct answer was " +numAns + ".";
}
score = "" + ((correct / (correct + wrong)) * 100);
score = score.substring(0,4) + "%";
alert(msg + "\n\nYOUR SCORE: " + score + "\n"
+ correct + " correct\n"
+ wrong + " incorrect")
}
// End -->
</script>
<title>Clint's kidsmath.p2</title>
</head><body>
<center>
<form name="quizform"><input value="add" onclick="add()" type="button">
<input value="subtract" onclick="subtract()" type="button"><input value="multiply" onclick="multiply()" type="button"> <input value="divide" onclick="divide()" type="button"><br>
<br>
<input name="arithmetic" type="radio">Easy <input name="arithmetic" checked="checked" type="radio">Moderate <input name="arithmetic" type="radio">Difficult <br>
<br>
Do not simpify your answer!<br>
<br>
<input value="Check Score" onclick="check()" type="button"> <input value="Reset Score" onclick="javascript :correct=0;wrong=0;" type="button"></form>
</center>
</body></html>
Last edited by csharp100; 04-14-2012 at 10:35 PM .
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?
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];
}
My function call is now:
Code:
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.
Last edited by csharp100; 04-15-2012 at 02:21 PM .
Ok got the print format, I just changed this line
Code:
return [numerator/gcd, denominator/gcd];
to
Code:
return [numerator/gcd + "/" + denominator/gcd];
Last edited by csharp100; 04-15-2012 at 03:30 PM .
What value does maxValue have if multiply() is not invoked?
Originally Posted by
WyCnet
What value does maxValue have if multiply() is not invoked?
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:
Math.floor(Math.random()*maxValue)+lowestvalue;
That'll give you a pseudorandom number, n, lowestvalue <= n < maxValue.
Code:
return [numerator/gcd + "/" + denominator/gcd];
Pointless to put it in an array, if you want it to return a string, return a string:
Code:
return numberator/gcd + "/" + denominator/gcd;
Great wit and madness are near allied, and fine a line their bounds divide.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks