Skip to content

Instantly share code, notes, and snippets.

@0xAliRaza
Last active March 8, 2022 13:54
Show Gist options
  • Save 0xAliRaza/e2c74fdfed75292ec08e0fe7adacd3e9 to your computer and use it in GitHub Desktop.
Save 0xAliRaza/e2c74fdfed75292ec08e0fe7adacd3e9 to your computer and use it in GitHub Desktop.
Print math table of given parameter in JS
/**
*
* Date: 08/03/2022
* Task: 04
* Author: Ali Raza (ali.dev@pk.see.biz)
*
*/
function printTable(num) {
// Check if a valid number
if (!Number.isInteger(num)) {
console.error("Please provide a valid number.");
return;
}
if (num <= 0) {
console.error("Provided number should be greater than zero.");
return;
}
const table = {};
// Fill all the table values in key=>value pairs
for (let i = 1; i <= 10; i++) {
table[`${num} x ${i}`] = +Number(num * i).toFixed(1);
}
console.table(table);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment