Skip to content

Instantly share code, notes, and snippets.

@arifullahjan
Created November 5, 2020 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arifullahjan/226e7884777ede9267a22e168993f746 to your computer and use it in GitHub Desktop.
Save arifullahjan/226e7884777ede9267a22e168993f746 to your computer and use it in GitHub Desktop.
/**
* 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