- 1). Open your HTML file in a text editor such as Windows Notepad.
- 2). Add a function that calculates a multiplication result through repeated addition by adding the following code inside the "<head>" part of the HTML file:
<script type="text/javascript">
function multiply() {
var a = parseInt(document.getElementById('a').value);
var b = parseInt(document.getElementById('b').value);
var c=0;
var count=0;
var sign = "";
if (((a<0) && (b>0)) || ((a>0) && (b<0))) {
sign = "-";
}
a = Math.abs(a);
for (count=1; count<=Math.abs(b); count++) {
c += a;
}
alert(sign + c.toString());
}
</script> - 3). Add a form within the "<body>" of your HTML file that will get two integers from the user and then call the multiplication function by adding the code:
<form>
1st number: <input type="text" name="a"/>
2nd number: <input type="text" name="b"/>
<input type="button" value="Multiply!" onclick="multiply()" />
</form>
The answer will popup in a box once you click the "Multiply!" button. For example, -4 and 1 will output -4. - 4). Save the HTML file and load on your web server to view the multiplication function.
SHARE