Created
November 5, 2020 19:20
-
-
Save arifullahjan/226e7884777ede9267a22e168993f746 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculates table for | |
* @param size | |
*/ | |
const calculateTable = (size: number): number[][] => { | |
const table = Array.apply(null, new Array(size)).map(() => []); // fill matrix | |
for (let i = 0; i < size; i++) { | |
for (let j = i; j < size; j++) { | |
const value = (j + 1) * (i + 1) | |
table[i][j] = value | |
table[j][i] = value | |
} | |
} | |
return table | |
} | |
/** | |
* Prints any grid to console | |
* @param table | |
*/ | |
const printTable = (table: number[][]): string => { | |
let output = '' | |
table.forEach(row => { | |
row.forEach(item => { | |
output += `${item} ` | |
}) | |
output += '\n' | |
}); | |
return output | |
} | |
/** | |
* Prints table grid of size | |
* @param size | |
*/ | |
const printTableQuick = (size: number): void => { | |
for (let i = 1; i <= size; i++) { | |
let row = '' | |
for (let j = 1; j <= size; j++) { | |
row += `${j * i} ` | |
} | |
console.log(row); | |
} | |
} | |
const table = calculateTable(12); | |
console.log(printTable(table)) | |
printTableQuick(12) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment