stricter syntax

This commit is contained in:
array-in-a-matrix 2022-02-09 14:49:55 -05:00
parent 226ca8cfa3
commit cd648685e1

View file

@ -1,29 +1,29 @@
const readline = require('readline').createInterface({ const readline = require('readline').createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout output: process.stdout
}) });
function dec_to_base() { function dec_to_base() {
readline.question(`Enter a number to convert: `, num => { readline.question(`Enter a number to convert: `, num => {
num = parseInt(num) num = parseInt(num);
readline.question(`Enter a number new base: `, base => { readline.question(`Enter a number new base: `, base => {
base = parseInt(base) base = parseInt(base);
try { try {
new_num = num.toString(base) new_num = num.toString(base);
console.log(`\nBASE 10: ${num} ==> BASE ${base}: ${new_num}`); console.log(`\nBASE 10: ${num} ==> BASE ${base}: ${new_num}`);
} catch (RangeError) { } catch (RangeError) {
console.log(`Base is not in the range of 2 to 36.\nExiting...`); console.log(`Base is not in the range of 2 to 36.\nExiting...`);
} finally { } finally {
readline.close() readline.close();
} };
}) });
}) });
} };
function base_to_dec() { function base_to_dec() {
readline.question(`Enter a number to convert: `, num => { readline.question(`Enter a number to convert: `, num => {
readline.question(`Enter the number's base: `, base => { readline.question(`Enter the number's base: `, base => {
base = parseInt(base) base = parseInt(base);
try { try {
new_num = parseInt(num, base); new_num = parseInt(num, base);
console.log(`\nBASE ${base}: ${num} ==> BASE 10: ${new_num}`); console.log(`\nBASE ${base}: ${num} ==> BASE 10: ${new_num}`);
@ -31,53 +31,53 @@ function base_to_dec() {
console.log(`Base is not in the range of 2 to 36.\nExiting...`); console.log(`Base is not in the range of 2 to 36.\nExiting...`);
} }
finally { finally {
readline.close() readline.close();
} };
}) });
}) });
} };
function base_to_base() { function base_to_base() {
readline.question(`Enter a number to convert: `, num => { readline.question(`Enter a number to convert: `, num => {
readline.question(`Enter the number's base: `, base => { readline.question(`Enter the number's base: `, base => {
base = parseInt(base) base = parseInt(base);
readline.question(`Enter a number new base: `, new_base => { readline.question(`Enter a number new base: `, new_base => {
new_base = parseInt(new_base) new_base = parseInt(new_base);
try { try {
dec_num = parseInt(num, base); let dec_num = parseInt(num, base);
new_num = dec_num.toString(new_base) let new_num = dec_num.toString(new_base);
console.log(`\nBASE ${base}: ${num} ==> BASE ${new_base}: ${new_num}`); console.log(`\nBASE ${base}: ${num} ==> BASE ${new_base}: ${new_num}`);
} catch (RangeError) { } catch (RangeError) {
console.log(`One or more bases is not in the range of 2 to 36.\nExiting...`); console.log(`One or more bases is not in the range of 2 to 36.\nExiting...`);
} }
finally { finally {
readline.close() readline.close();
} };
}) });
}) });
}) });
} };
readline.question(`Choose an option:\n\n1) decimal to any base\n2) any base to decimal\n3) any base to any base\n`, choice => { readline.question(`Choose an option:\n\n1) decimal to any base\n2) any base to decimal\n3) any base to any base\n`, choice => {
if (choice * 1.0) { if (choice * 1.0) {
choice = parseInt(choice); choice = parseInt(choice);
switch (choice) { switch (choice) {
case 1: case 1:
dec_to_base() dec_to_base();
break; break;
case 2: case 2:
base_to_dec() base_to_dec();
break; break;
case 3: case 3:
base_to_base() base_to_base();
break; break;
default: default:
console.log("Please choose a proper entry.") console.log("Please choose a proper entry.");
readline.close() readline.close();
break; break;
} };
} else { } else {
console.log("Please enter a number.") console.log("Please enter a number.");
readline.close() readline.close();
} };
}) });