/    Sign up×
Community /Pin to ProfileBookmark

MOD function in calculator

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.

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliSep 10.2004 — [i]Originally posted by gilgalbiblewhee [/i]

[B]

remainder= x MOD 22

[/B]
[/QUOTE]


this should work just the way you intend...(n % 22 )

remainder= (x % 22 );
Copy linkTweet thisAlerts:
@steelersfan88Sep 10.2004 — Or you can add it to make a little more complex, in order to store the quotient, which is a big extension to what Khalid explained:<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).

Dr. Script
Copy linkTweet thisAlerts:
@gilgalbiblewheeauthorSep 10.2004 — I don't know much about Javascript. Isn't there a code to insert in the script in the head?

Where does your code fit in?
Copy linkTweet thisAlerts:
@steelersfan88Sep 10.2004 — Just in your example, replace MOD with the % operator.

Or use my example, in which to perform MOD, use new Mod(dividend, divisor);. Then the value of the variable is set to the remainder. You can also access the quoptient by the variable name.quotient.
Copy linkTweet thisAlerts:
@gilgalbiblewheeauthorSep 10.2004 — Like this?

[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]
Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — No, the lines: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.
Copy linkTweet thisAlerts:
@gilgalbiblewheeauthorSep 11.2004 — I don't understand it.
Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — What do you wnat to do? Why would you even think about writing "I don't understand." It is obvious to all of us that is the reason you asked.

Tell us what you don't understand, and how you plan to use it, or we won't be able tomake you understand.

... and stop thinking all the work will be done for you
Copy linkTweet thisAlerts:
@Khalid_AliSep 11.2004 — [i]Originally posted by gilgalbiblewhee [/i]

[B]I don't understand it. [/B][/QUOTE]


no problem, I did post a very simple suggestion earlier ..anyways..here it is again with full working example....

Just keep in mind the the % sign is used instead of VB Mod function in most of the programming languages.

<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--
function Process(){
var frm = document.getElementById("form1");
var x = Number(frm.t1.value);
var remainder = (x % 22);
alert("( " + x + " % 22 ) = " + remainder)
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;

&lt;body class="body"&gt;
&lt;form id="form1" action="" onsubmit=""&gt;
&lt;input type="text" name="t1"/&gt;
&lt;input type="button" value="process" onclick="Process()"/&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — That'll work, just curious why you didn't pass the form as an argument ... probably a tad easier IMO.
Copy linkTweet thisAlerts:
@Khalid_AliSep 11.2004 — [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]


is your question directed to me? just curious?
Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — Yes ... I would think it would be easier to send the form as a reference.

Directed to the OP, if you did:&lt;script type="text/javascript"&gt;
&lt;!--
function Process(frm){
var x = Number(frm.t1.value);
var remainder = (x % 22);
alert("( " + x + " % 22 ) = " + remainder)
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;

&lt;body class="body"&gt;
&lt;form id="form1" action="" onsubmit=""&gt;
&lt;input type="text" name="t1"/&gt;
&lt;input type="button" value="process" onclick="Process(this.form)"/&gt;
&lt;/form&gt;
Dr. Script
Copy linkTweet thisAlerts:
@Khalid_AliSep 11.2004 — [i]Originally posted by steelersfan88 [/i]

[B]Yes ....Dr. Script [/B][/QUOTE]


First of all saying that its easy is completely relative term.

if you scroll up you will see that you wrote your self that..

[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]


Which means something that is obvious to you may not be as obvious to others.

Second as far as passing form reference as parameter or getting it in the function

is concern the it has no bearing on performance of the code.

The point here to get a reference to the form, how you get it is really a practice,

as matter of fact this is so redundant to talk about this issue that I have never thought about it,

I have a template html page that is set like that..

Now in this case user said in explicit words what you thought was easy ,does not make any

sense for the member who posted the question.You have to remember that people who post

questions in these forums are mostly those who are learning JavaScript and usually are at the

first step of the ladder. And if you write a bit complext code it may not make any sense for them

Last but the not the least, you have tendency to get into such arguments,which has no usefullnes

for member who asked a question.I suggest we refrain from making this thread as such.

Thanks
Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — We are not making this thread an argument. Argumentative posts tend to do that. LEt me ask you a question? Why do you think gilgalbiblewhee posted this question? I think the answer is obvious enough, that gilgalbiblewhee did not understand, and no offense, so he (?) did the correct thing in presenting the question. The simple fact that he (?) posted that he (?) did not udnerstand was a given, otherwise there wouldn't have been this thread.

Easy is a relative term, and as a result, I used it that way. Easy in the way I have used it refers to shorter coding. Not as a term that means it is easier for a user, such as gilgalbiblewhee, to understand. 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
Copy linkTweet thisAlerts:
@Khalid_AliSep 11.2004 — [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]


you have got to be joking...right?

If you are implying that multiple forms in one page is a practice you want to cater then...I can only laugh...

Make note of it.

There must be only one form (having multiple form means the logic is seriously flawed) in page for a good coding practice...anyways..there is no use of continueing this conversation...
Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — I would not continue this conversation, except you seem to be misinterpreting what I say completely. Let's make this script modular, so it'll be in a .js file. We have two pages, modulus.htm, and mod.htm. Both do the same thing, except the names on the forms are different.

So ... do you send the form reference, or do you make 2 functions, each with a different name. I think we all know which one is the better choice.

I never kid. Kidding was so 15 years ago.

Dr. Script
×

Success!

Help @gilgalbiblewhee 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 6.1,
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,
)...