Arithmetic/Integer: Difference between revisions

Content added Content deleted
m (→‎{{header|JavaScript}}: spidermonkey's javascript implementation has print/readline)
Line 446: Line 446:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
In order to get user input, this solution {{works with|JScript}}, however the arithmetic operators are the same for any version of JavaScript.
In order to get user input, this solution {{works with|JScript}} or {{works with|SpiderMonkey}}, however the arithmetic operators are the same for any version of JavaScript.


Note that JavaScript division returns a float, even if the operands are integers.
Note that JavaScript division returns a float, even if the operands are integers.
<lang javascript>function get_input(prompt) {
<lang javascript>var a = parseInt(get_input("Enter an integer"), 10);
WScript.Echo(prompt);
return WScript.StdIn.readLine();
}

var a = parseInt(get_input("Enter an integer"), 10);
var b = parseInt(get_input("Enter an integer"), 10);
var b = parseInt(get_input("Enter an integer"), 10);


Line 463: Line 458:
WScript.Echo("product: a * b = " + (a * b));
WScript.Echo("product: a * b = " + (a * b));
WScript.Echo("quotient: a / b = " + (a / b));
WScript.Echo("quotient: a / b = " + (a / b));
WScript.Echo("remainder: a % b = " + (a % b));</lang>
WScript.Echo("remainder: a % b = " + (a % b));

function get_input(prompt) {
output(prompt);
try {
return WScript.StdIn.readLine();
} catch(e) {
return readline();
}
}
function output(prompt) {
try {
WScript.Echo(prompt);
} catch(e) {
print(prompt);
}
}</lang>
output:
output:
<pre>Enter an integer
<pre>d:\glennj>cscript basic_arith.js
cscript basic_arith.js
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Enter an integer
-147
-147
Enter an integer
Enter an integer