Menu
I would like to know the Javascript for the MOD function in a calculator.
Let’s say I have the constant 22, and x is a variable:
remainder= x MOD 22
So if x is higher than 22, let it divide. The answer I’m looking for is the quotient, the letter R (for remainder) and the remainder.
[i]Originally posted by gilgalbiblewhee [/i]
[B]
remainder= x MOD 22
[/B][/QUOTE]
<script type="text/javascript">
function Mod(a,b) {
this.remainder = a%b;
this.quotient = (a-(a%b))/(b);
}
Mod.prototype.valueOf = function() { return this.remainder; }
Mod.prototype.toString = function() { return this.remainder; }
Mod.prototype.alert = function() { alert("Remainder: "+ this +"nQuotient: "+ this.quotient); }
var rem = new Mod(50,22); // 50 is the dividend, 22 is the divisor (50/22)
rem.alert(); // alerts the remainder and quotient (6 and 2, respectively)
</script>
The remainder, stored in Mod.remainder, is the value when displaying Mod as a string or a number. The quotient is stored in Mod.quotient (Mod is the name of the variable).[CODE]
<HEAD>
<SCRIPT>
[B]
function Mod(a) {
this.remainder= (a % 22 );
this.quotient = (a-(a%22))/(22);
}
[/B]
Mod.prototype.valueOf = function() { return this.remainder; }
Mod.prototype.toString = function() { return this.remainder; }
Mod.prototype.alert = function() { alert("Remainder: "+ this +"nQuotient: "+ this.quotient); }
var rem = new Mod(50,22); // 50 is the dividend, 22 is the divisor (50/22)
rem.alert(); // alerts the remainder and quotient (6 and 2, respectively)
-->
</SCRIPT>
</HEAD>
<BODY>
[B] <TD align=center><INPUT type=button name="Mod" value=" +/- "></TD>[/B]
</BODY>
[/CODE]
var rem = new Mod(50,22); // 50 is the dividend, 22 is the divisor (50/22)
rem.alert(); // alerts the remainder and quotient (6 and 2, respectively)
are the line sthat you use. The button could call a function that would do this, but that is the process above.[i]Originally posted by gilgalbiblewhee [/i]
[B]I don't understand it. [/B][/QUOTE]
<i>
</i><script type="text/javascript">
<!--
function Process(){
var frm = document.getElementById("form1");
var x = Number(frm.t1.value);
var remainder = (x % 22);
alert("( " + x + " % 22 ) = " + remainder)
}
//-->
</script>
</head>
<body class="body">
<form id="form1" action="" onsubmit="">
<input type="text" name="t1"/>
<input type="button" value="process" onclick="Process()"/>
</form>
[i]Originally posted by steelersfan88 [/i]
[B]That'll work, just curious why you didn't pass the form as an argument ... probably a tad easier IMO. [/B][/QUOTE]
<script type="text/javascript">
<!--
function Process(frm){
var x = Number(frm.t1.value);
var remainder = (x % 22);
alert("( " + x + " % 22 ) = " + remainder)
}
//-->
</script>
</head>
<body class="body">
<form id="form1" action="" onsubmit="">
<input type="text" name="t1"/>
<input type="button" value="process" onclick="Process(this.form)"/>
</form>
Dr. Script[i]Originally posted by steelersfan88 [/i]
[B]Yes ....Dr. Script [/B][/QUOTE]
[i]Originally posted by steelersfan88 [/i]
[B]What do you wnat to do? Why would you even think about writing
"I don't understand." It is obvious to all of us Dr. Script [/B][/QUOTE]
[i]Originally posted by steelersfan88 [/i]
[B] I questioned why you did not send the form in the example, making your script much, much more available to several forms, and more globally available. More than one form with same script, rather than a different function for each form.
Dr. Script [/B][/QUOTE]
0.1.9 — BETA 6.1