Click to See Complete Forum and Search --> : Simple multiplication table


razultull
11-01-2010, 04:51 PM
Hey so I'm trying to create a dynamic table in javascript such that you have an HTML file with a form, for a webpage im making.


<form action="table" method="get">
<label>
Start Number:
<input type="text" name="start" size="10"/>
</label>

<label>
End Number:
<input type="text" name="end" size="10"/>
</label>

<label>
Limit:
<input type="text" name="limit" size="10"/>
</label>

<input type="submit" name="mult" value="Create this table"/>
</form>




This html is separate from the servlet i am trying to write that will exist in a jsp/java file.

Basically the output should be something like

<table>
<tr><td>4*0=0</td><td>4*1=4</td><td>4*2=8</td></tr>
<tr><td>5*0=0</td><td>5*1=5</td><td>5*2=10</td></tr>
<tr><td>6*0=0</td><td>6*1=6</td><td>6*2=12</td></tr>
</table>



but embedded....
There are THREE inputs by the user, start, end , limit.


I think i have got a general idea of how to do it, but am a bit fuzzy on how to implement the logic with the actual printing of the html in the document.
i know i have to use for loops but am unsure how.
This is the method i would like to adhere to, i dont want to use inline javascript because there is going to be some flash in there as well.


this is the javascript i have come up with so far


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

Public class Table extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

public class Counter extends HttpServlet {
private int v;
private List<String> starts;
private List<String> ends;
private List<String> mults;

public void init() {
v = 0;
starts = new ArrayList<String>();
ends = new ArrayList<String>();
mults = new ArrayList<String>();
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

String start = req.getParameter("start");
String end = req.getParameter("end");
String mult = req.getParameter("mult");

for(int i = 1; i <= start ; i++)
{

}


}

PrintWriter o = res.getWriter();
String r =
"<html><head><title>Multiplication Result</title></head>" +
;

}



Thanks in advance to anyone who can help.

JMRKER
11-01-2010, 08:57 PM
You are in the 'javascript' forum with a 'java' question.

Don't know if this is what you want, but here is a 'javascript' ONLY solution.


<html>
<head>
<title>Multiplication Table</title>
<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?t=237789

function CreateMultTable() {
var RowStart = Number(document.getElementById('start').value);
var RowLimit = Number(document.getElementById('end').value);
var RowWidth = Number(document.getElementById('wide').value);
var str = '';
str += '<table border="1">';
for (var y=RowStart; y<=RowLimit; y++) {
str += '<tr>';
for (var x=0; x<=RowWidth; x++) {
str += '<td>'+y+' * '+x+' = '+(x*y)+'</td>';
}
str += '</tr>';
}
str += '</table>';
document.getElementById('tblDisplay').innerHTML = str;
return false;
}
</script>
</head>
<body>
<form action="table" method="get" onsubmit="return CreateMultTable()">
Start Number: <input type="text" name="start" size="10" id='start' value='0'> <br>
End Number: <input type="text" name="end" size="10" id='end' value='20'> <br>
Limit: <input type="text" name="limit" size="10" id='wide' value='10'> <br>
<input type="submit" name="mult" value="Create this table"> <p>&nbsp;
<div id="tblDisplay"></div>
</form>
</body>
</html>