/    Sign up×
Community /Pin to ProfileBookmark

Javascript help: a self updating multiplication table?

well i made a multiplication table with the following code:

[code]
<script type=”text/javascript”>
var numRows = parseInt( prompt(“Enter number of rows “, “10”) );
var numCols = parseInt( prompt(“Enter number of columns”, “10”) );
if (isNaN(numRows) || isNaN(numCols)) {
alert(“Rows and columns must be a number”);
}else if (numRows < 1 || numCols < 1) {
alert(“Rows and columns must be greater than zero”);
}else if (numRows * numCols > 10000) {
alert(“Too many rows or columns”);
}else {
var tblHTML = “”, rowHTML;
for (var row = 0; row <= numRows; row++) {
rowHTML = “”;
for (var col = 0; col <= numCols; col++) {
if (row == 0) {
rowHTML += “<td><strong>” +
((col == 0) ? ” ” : col) +
“</strong> </td>”;
} else if (col == 0) {
rowHTML += “<td><strong>” +
row +
“</strong> </td>”;
} else {
rowHTML += “<td>” +
(row * col) +
“</td>”;
}
}
tblHTML += “<tr>” + rowHTML + “</tr>”;
}
document.write(“<table border=”1″>” +
tblHTML +
“</table>”);
}
</script>
[/code]

but now my assignment calls for me to In stead of storing the numbers 1 thru 12 in an array… store random numbers… create a 12 x 12 multiplication table that recalculates every 10 seconds using a timer. and im allitttle confused. any help would be much apprrreeeciated. thanks guys!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 02.2009 — Your assignment does not make much sense.

  • 1. You did not use an array to store numbers 1 thru 12 anywhere in your program.

  • 2. A 12x12 multiplication table does not change ... 1 thru 12 times 1 thru 12 is always the same.

  • 3. Recalculating ever 10 seconds will not change the table.

  • 4. Random number storage is easy, but is it supposed to replace the user prompts?
    <i>
    </i>rndRows = Math.random() * numRows;
    rndCols = Math.random() * numCols;

    Use in place of your numRows and numCols in the FOR(...) loops,

    but that does not limit to a 12x12 table display.
  • Copy linkTweet thisAlerts:
    @AndyVengeanceauthorNov 02.2009 — ill copy and paste the actual assignment:

    Part A:

    Multiplication table (1 thru 12). Use two arrays (length of 12) and two for loops to create a 12 x 12 multiplication table.

    See the Assignment5.png file to see what it should look like.

    Part B:

    Multiplication table w/random numbers every 10

    seconds. In stead of storing the numbers 1 thru 12 in an array... store random numbers... create a 12 x 12 multiplication table.

    Have the table automatically recalculate every 10 seconds or so (use a timer).
    Copy linkTweet thisAlerts:
    @AndyVengeanceauthorNov 02.2009 — pretty confused cause when i replace rndRows = Math.random() * numRows;

    rndCols = Math.random() *
    numCols; with the ones in the for loops i get errors.

    excuse me.. im not much of a javascript guy
    Copy linkTweet thisAlerts:
    @JMRKERNov 02.2009 — Replace NOT in the for loops...
    <i>
    </i>// var numRows = parseInt( prompt("Enter number of rows ", "10") );
    // var numCols = parseInt( prompt("Enter number of columns", "10") );

    var numRows = Math.random() * 12; // for maximum of 12x12 table (often smaller)
    var numCols = Math.random() * 12;

    ×

    Success!

    Help @AndyVengeance spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...