Jump to content

Arithmetic/Integer: Difference between revisions

m
→‎{{header|JavaScript}}: spidermonkey's javascript implementation has print/readline
m (→‎{{header|JavaScript}}: spidermonkey's javascript implementation has print/readline)
Line 446:
 
=={{header|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.
<lang javascript>functionvar a = parseInt(get_input(prompt"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);
 
Line 463 ⟶ 458:
WScript.Echo("product: a * b = " + (a * b));
WScript.Echo("quotient: a / b = " + (a / b));
WScript.Echo("remainder: a % b = " + (a % b));</lang>
 
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:
<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
Enter an integer
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.