User Tools

Site Tools


java-script:multiplication-table-generator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java-script:multiplication-table-generator [2023/08/03 01:25] odeftajava-script:multiplication-table-generator [2023/08/09 20:14] (current) – ↷ Links adapted because of a move operation odefta
Line 1: Line 1:
 ====== Multiplication Table Generator ====== ====== Multiplication Table Generator ======
  
-This is a simple script to print a multiplication table generator using simple java script and html. +===== For client side (web browser) ===== 
-Ex+ 
-<code> +This is a simple script to print a multiplication table generator using simple java script and html. \\ 
-1 2 3 +Output example
-2 4 6 + 
-3 6 9 +{{:java-script:pasted:20230803-022409.png}}
-</code>+
  
-All code js + html in the same file:+==== All code js + html in the same file ====
  
 <code javascript index.html> <code javascript index.html>
Line 23: Line 22:
                     html = html + '<tr>';                     html = html + '<tr>';
  
-                    for(let columnNumber = 1; columnNumber <= rows; columnNumber++) {+                    for(let columnNumber = 1; columnNumber <= columns; columnNumber++) {
                         html = html + `<td>${rowNumber * columnNumber}</td>`;                         html = html + `<td>${rowNumber * columnNumber}</td>`;
                     }                     }
Line 67: Line 66:
 </code> </code>
  
-Js code separated from html code:+==== Js code separated from html code ====
  
 <code html index.html> <code html index.html>
Line 96: Line 95:
         html = html + '<tr>';         html = html + '<tr>';
  
-        for(let columnNumber = 1; columnNumber <= rows; columnNumber++) {+        for(let columnNumber = 1; columnNumber <= columns; columnNumber++) {
             html = html + `<td>${rowNumber * columnNumber}</td>`;             html = html + `<td>${rowNumber * columnNumber}</td>`;
         }         }
Line 126: Line 125:
 </code> </code>
  
 +===== For server side (node.js) =====
  
 +First you need to [[.node-js:setup|setup your node.js project]]
 +
 +Then create a src folder and inside it place index.js and generateTable.js:
 +
 +<code javascript generateTable.js>
 +export function createMultiplicationTable(rows, columns) {
 +    let tableString = '';
 +
 +    for(let rowNumber = 1; rowNumber <= rows; rowNumber++) {
 +        for(let columnNumber = 1; columnNumber <= columns; columnNumber++) {
 +            tableString = tableString + ` ${rowNumber * columnNumber} `;
 +        }
 +        tableString = tableString + '\n';     
 +    }
 +
 +    return tableString;
 +}
 +</code>
 +
 +<code javascript index.js>
 +import { createMultiplicationTable } from "./generateTable.js";
 +
 +let args = process.argv.slice(2);
 +
 +let rowsInput = args[0];
 +let columnsInput = args[1];
 +
 +let rows = Number(rowsInput);
 +let columns = Number(columnsInput);
 +
 +if (isNaN(rows) || isNaN(columns)) {
 +    console.log("Invalid input! Please enter numbers only.");
 +}
 +else {
 +    let tableString = createMultiplicationTable(rows, columns);
 +    console.log(tableString);
 +}
 +</code>
 +
 +To run the code, from the project root folder:
 +
 +<code>
 +node src/index.js 5 5
 +</code>
 +
 +Will output in the console:
 +
 +<code>
 +1  2  3  4  5     
 +2  4  6  8  10    
 +3  6  9  12  15   
 +4  8  12  16  20  
 +5  10  15  20  25 
 +</code>
java-script/multiplication-table-generator.1691015148.txt.gz · Last modified: 2023/08/03 01:25 by odefta