View difference between Paste ID: Abvup1W8 and Sn451RaQ
SHOW: | | - or go back to the newest paste.
1
<html>
2
<HEAD>
3
 <style type='text/css'>
4
            body {
5
                font-family: "Chalkduster";
6
            }
7
            h1, h2, h3, h4, h5, h6 {
8
              display: inline;
9
              margin: 0;
10
              font-weight: normal;
11
            }
12
            input
13
{
14
	font-family: "Chalkduster";
15
	font-size:0.95em;
16
}
17
18
        </style>
19
20
21
<script type="text/javascript">
22
<!--
23
/* This script and many more are available free online at
24
The JavaScript Source!! http://www.javascriptsource.com
25
Created by: Matthew Roy :: http://matthewroy.com */
26
27
/*****************************************
28
**   Matthew C. Roy
29
**   Big Business Websites
30
**   For Small Business Prices!
31
**   http://www.matthewroy.com
32
**
33
**   You may use this script freely for
34
**   non-commercial use as long
35
**   as this header is left intact.
36
*****************************************/
37
38
var n1, n2, d1, d2, An, Ad, Op;
39
var int 
40
var neg=1;
41
42
function solve(){
43
  //If all fields are numbers
44
  if(!isNaN(document.calc.n1.value)&&!isNaN(document.calc.d1.value)&&!isNaN(document.calc.n2.value)&&!isNaN(document.calc.d2.value)){
45
  //If no fields are blank
46
  if(document.calc.n1.value!=''&&document.calc.d1.value!=''&&document.calc.n2.value!=''&&document.calc.d2.value!=''){
47
    //Set variables:
48
    n1=document.calc.n1.value;// Numerator 1
49
    d1=document.calc.d1.value;// Numerator 2
50
    n2=document.calc.n2.value;// Denominator 1
51
    d2=document.calc.d2.value;// Denominator 2
52
    Op=document.calc.Op.value;// Operator
53
    } else {
54
    //If blank field
55
    alert('Please fill-in all fields!');
56
    }
57
  } else {
58
  //If field has non-number
59
  alert('Please enter only Numbers into the fields!');
60
  }
61
62
  //Which Operation
63
  switch (Op){
64
  case '+':
65-
    An=(n1*d2)+(n2*d1) //Answer Numerator
65+
66-
    Ad=(d1*d2) //Answer Denominator
66+
    if (d1==d2){
67
      Ad=d1; //Answer Denominator
68
      An=parseInt(n1)+parseInt(n2); //Answer Numerator
69
    }
70
    else{
71
      if (Math.max(d1,d2) % Math.min(d1,d2) == 0){
72
        Ad=Math.max(d1,d2) //Answer Denominator
73
        An=parseInt(Ad/d1*n1) + parseInt(Ad/d2*n2) //Answer Numerator
74
      }
75
      else{
76-
    An=(n1*d2)-(n2*d1)//Answer Numerator
76+
        Ad=(d1*d2) //Answer Denominator
77-
    Ad=(d1*d2)//Answer Denominator
77+
        An=parseInt(n1*d2)+parseInt(n2*d1) //Answer Numerator
78
      }
79
    }
80
    if(document.calc.reduce.checked==1){
81
      reduce();
82
    } else {
83
      display();
84
    }
85
   break
86
87
  case '-':
88
    //subtract fractions using formula ((n1*d2)-(n2*d1)) over (d1*d2)
89
    if (d1==d2){
90
      Ad=d1; //Answer Denominator
91
      An=(n1-n2); //Answer Numerator
92
    }
93
    else{
94
      if (Math.max(d1,d2) % Math.min(d1,d2) == 0){
95
        Ad=Math.max(d1,d2) //Answer Denominator
96
        An=Ad/d1*n1 - Ad/d2*n2 //Answer Numerator
97
      }
98
      else{
99
        Ad=(d1*d2) //Answer Denominator
100
        An=(n1*d2)-(n2*d1) //Answer Numerator
101
      }
102
    }
103
    if(document.calc.reduce.checked==1){
104
      reduce();
105
    } else {
106
      display();
107
    }
108
   break
109
110
  case '*':
111
    //multiply fractions using formula (n1*n2) over (d1*d2)
112
    An=n1*n2;//Answer Numerator
113
    Ad=d1*d2; //Answer Denominator
114
    if(document.calc.reduce.checked==1){
115
            reduce();
116
    } else {
117
      display();
118
    }
119
    break
120
121
  case '/':
122
    //divide fractions using formula (n1*d2) over (d1*n2)
123
    An=n1*d2;//Answer Numerator
124
    Ad=d1*n2;//Answer Denominator
125
    if(document.calc.reduce.checked==1){
126
      reduce();
127
    } else {
128
      display();
129
    }
130
   break
131
  }
132
}
133
134
function reduce() {
135
  neg=1; //1 if positive, -1 if negative
136
  //convert to strings
137
  ng=An+'';
138
  dg=Ad+''
139
  if(ng.indexOf('-')!=-1){  //check to see if answer is negative.
140
    neg=-1
141
  }
142
  if(dg.indexOf('-')!=-1){
143
    neg=-1
144
  }
145
  if(ng.indexOf('-')!=-1&&dg.indexOf('-')!=-1)  {//if both numerator and denominator are negative the answer is positive
146
    neg=1
147
  }
148
  var factorX //highest common factor
149
150
  if ( An == 0 || Ad == 0 ) {
151
    factorX=1;
152
    return;
153
  }
154
155
  An = Math.abs( An );
156
  Ad = Math.abs( Ad );
157
158
  var factorX = 1;
159
160
  //Find common factors of Numerator and Denominator
161
  for ( var x = 2; x <= Math.min( An, Ad ); x ++ ) {
162
    var check1 = An / x;
163
    if ( check1 == Math.round( check1 ) ) {
164
      var check2 = Ad / x;
165
      if ( check2 == Math.round( check2 ) ) {
166
        factorX = x;
167
      }
168
    }
169
  }
170
171
  An=(An/factorX)*neg;  //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative
172
  Ad=Ad/factorX;  //divide by highest common factor to reduce fraction
173
  display();
174
}
175
176
function display(){
177
  //Display answer
178
  document.calc.An.value = An;
179
  document.calc.Ad.value = Ad;
180
}
181
182
// -->
183
</script>
184
</HEAD>
185
186
<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->
187
188
<BODY>
189
190
<table width="250" align="center" border="0" cellspacing="0" cellpadding="1" style="background-color:#ffffff;border:1px #000000 solid;">
191
  <tr>
192
    <td align="center" valign="middle">
193
      <h3>Calculadora de Quebrados</h4>
194
      <form name="calc">
195-
</table>
195+
196
          <tr>
197
            <td style="border-bottom:4px #000000 solid;"><input type="text" size="1"  name="n1" id="n1" tabindex="1"></td>
198
            <td rowspan="2" align="center" valign="middle">
199
              <select name="Op" tabindex="3">
200
              <option value="+">+</option>
201
              <option value="-">-</option>
202
              <option value="*">x</option>
203
              <option value="/">÷</option>
204
              </select>
205
            </td>
206
            <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="n2" id="n2" tabindex="4"></td>
207
            <td rowspan="2" align="center" valign="middle"><input type="button" value=" = "onClick="solve();" tabindex="6"></td>
208
            <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="An" id="An" readonly="1"></td>
209
          </tr>
210
          <tr>
211
            <td><input type="text" size="1" name="d1" id="d1" tabindex="2"></td>
212
            <td><input type="text" size="1" name="d2" id="d2" tabindex="5"></td>
213
            <td><input type="text" size="1" name="Ad" id="Ad" readonly="1"></td>
214
        </tr>
215
        </table>
216
      <br><input type="checkbox" name="reduce" id="reduce" checked> Reducir
217
    </form>
218
    </td>
219
  </tr>
220
</table>
221
222
</body>
223
</html>