1. Registered User
Join Date
Feb 2012
Posts
9

## 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);
Answer=window.prompt( n1 + "/" + d1 + " * " + n2 + "/" + d2 + " = ", 0);
ans();
}```
If you need more of the program let me know.

2. Registered User+
Join Date
Aug 2007
Posts
3,767
Code:
`n1=random(maxValue);`
There probably. And, furthermore, unless you've defined random yourself, you're looking for
Code:
`Math.floor(Math.random()*maxValue);`

3. Registered User
Join Date
Apr 2012
Posts
55
"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);
Answer=window.prompt( n1 + "/" + d1 + " * " + n2 + "/" + d2 + " = ", 0);
ans();
}```

4. Registered User
Join Date
Feb 2012
Posts
9
Originally Posted by Declan1991
Code:
`n1=random(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.

5. Registered User
Join Date
Feb 2012
Posts
9
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);
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">

<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) &#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);
}

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));
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));
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);
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);
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) + "%";
+ correct + " correct\n"
+ wrong + " incorrect")
}
else alert("You have not completed any exercises yet.");
}

function ans() {
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) + "%";
+ correct + " correct\n"
+ wrong + " incorrect")
}

//  End -->
</script>
<title>Clint's kidsmath.p2</title>

<center>
<input value="subtract" onclick="subtract()" type="button"><input value="multiply" onclick="multiply()" type="button"> <input value="divide" onclick="divide()" type="button"><br>
<br>
<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.

6. Registered User
Join Date
Feb 2012
Posts
9
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&#37;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.

7. Registered User
Join Date
Feb 2012
Posts
9
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.

8. What value does maxValue have if multiply() is not invoked?

9. Registered User
Join Date
Feb 2012
Posts
9
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:
Code:
`maxValue=6;`

10. Registered User+
Join Date
Aug 2007
Posts
3,767
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;`

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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.27322 seconds
• Memory Usage 2,955KB
• Queries Executed 15 (?)
Template Usage (34):
• (16)bbcode_code
• (3)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (10)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (10)postbit
• (10)postbit_onlinestatus
• (10)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete