I assume you’re totally new in web development. If it is not the case, please don’t feel insulted by this answer.
First of all, you need a HTML element to write your output. Let’s use a PRE.
Now you need to access it with your Javascript. So your PRE need an id
attribute to identify it. Your HTML page must then have this line :
<PRE id="my-output"></PRE>
You can get this element in Javascript with the following code:
var pre = document.getElementById("my-output");
And to put HTML inside, just use the attribute innerHTML
, like this:
pre.innerHTML = "Hello <b>world</b>!";
Now you just need a for
loop to write each line of your calculus:
var n = 10;
pre.innerHTML = "";
for (var i=1 ; i<=n ; i++) {
pre.innerHTML += i + " * 2 = " + (i * 2) + "\n";
}
The \n
is a line-break that works in a PRE element.
Putting all together, you get this web page:
<html>
<head>
<script>
var n = 10;
function start() {
var pre = document.getElementById("my-output");
pre.innerHTML = "";
for (var i=1 ; i<=n ; i++) {
pre.innerHTML += i + " * 2 = " + (i * 2) + "\n";
}
}
</script>
</head>
<body>
<pre id="my-output">Click the button to see the resulting table.</pre>
<button onclick="start()">Show the table</button>
</body>
</html>
1
solved program for multiple of “2” in javascript [closed]