java-script:multiplication-table-generator
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
java-script:multiplication-table-generator [2023/08/03 01:18] – created odefta | java-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: | + | |
- | < | + | 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 | + | {{: |
- | </ | + | |
- | 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 + '< | html = html + '< | ||
- | for(let columnNumber = 1; columnNumber <= rows; columnNumber++) { | + | for(let columnNumber = 1; columnNumber <= columns; columnNumber++) { |
html = html + `< | html = html + `< | ||
} | } | ||
Line 67: | Line 66: | ||
</ | </ | ||
+ | ==== Js code separated from html code ==== | ||
+ | <code html index.html> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <script src=" | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | Number of rows: <input id = " | ||
+ | </ | ||
+ | < | ||
+ | Number of columns: <input id = " | ||
+ | </ | ||
+ | <button onclick=" | ||
+ | <div id=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <code javascript generateTable.js> | ||
+ | function createMultiplicationTable(rows, | ||
+ | let html = '< | ||
+ | |||
+ | for(let rowNumber = 1; rowNumber <= rows; rowNumber++) { | ||
+ | html = html + '< | ||
+ | |||
+ | for(let columnNumber = 1; columnNumber <= columns; columnNumber++) { | ||
+ | html = html + `< | ||
+ | } | ||
+ | |||
+ | html = html + '</ | ||
+ | } | ||
+ | |||
+ | |||
+ | html = html + '</ | ||
+ | return html; | ||
+ | } | ||
+ | |||
+ | function onGeneratePressed() { | ||
+ | let rowsInput = document.getElementById(" | ||
+ | let columnsInput = document.getElementById(" | ||
+ | |||
+ | let rows = Number(rowsInput); | ||
+ | let columns = Number(columnsInput); | ||
+ | |||
+ | if (isNaN(rows) || isNaN(columns)) { | ||
+ | alert(" | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | let tableHTML = createMultiplicationTable(rows, | ||
+ | |||
+ | document.getElementById(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== For server side (node.js) ===== | ||
+ | |||
+ | First you need to [[.node-js: | ||
+ | |||
+ | Then create a src folder and inside it place index.js and generateTable.js: | ||
+ | |||
+ | <code javascript generateTable.js> | ||
+ | export function createMultiplicationTable(rows, | ||
+ | let tableString = ''; | ||
+ | |||
+ | for(let rowNumber = 1; rowNumber <= rows; rowNumber++) { | ||
+ | for(let columnNumber = 1; columnNumber <= columns; columnNumber++) { | ||
+ | tableString = tableString + ` ${rowNumber * columnNumber} `; | ||
+ | } | ||
+ | tableString = tableString + ' | ||
+ | } | ||
+ | |||
+ | return tableString; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code javascript index.js> | ||
+ | import { createMultiplicationTable } from " | ||
+ | |||
+ | 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(" | ||
+ | } | ||
+ | else { | ||
+ | let tableString = createMultiplicationTable(rows, | ||
+ | console.log(tableString); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | To run the code, from the project root folder: | ||
+ | |||
+ | < | ||
+ | node src/ | ||
+ | </ | ||
+ | |||
+ | Will output in the console: | ||
+ | |||
+ | < | ||
+ | 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 | ||
+ | </ |
java-script/multiplication-table-generator.1691014682.txt.gz · Last modified: 2023/08/03 01:18 by odefta