Create Simple Calculator With cml (Comand Line): For Node js
By Ahmad Afan Shobari
Updated
•2 min read
Assalamualaikum, today we are learning about create simple calculator with command line, lets go to we are project, before it read
1. Add File Calculator
in this segmen we will create new file the name is calculator.js

2. Create Code In File
this code for we segmen
import { getinput, closeInput } from "./input.js";
console.clear();
console.log("=== Kalkulator Console ===");
let jalan = true;
while (jalan) {
console.log("\nPilih operasi:");
console.log("1. Tambah");
console.log("2. Kurang");
console.log("3. Kali");
console.log("4. Bagi");
console.log("0. Keluar");
const pilihan = await getinput("Masukkan pilihan (0-4)");
switch (pilihan) {
case "1": {
const a = Number(await getinput("Masukkan angka pertama"));
const b = Number(await getinput("Masukkan angka kedua"));
console.log(`Hasil: ${a} + ${b} = ${a + b}`);
break;
}
case "2": {
const a = Number(await getinput("Masukkan angka pertama"));
const b = Number(await getinput("Masukkan angka kedua"));
console.log(`Hasil: ${a} - ${b} = ${a - b}`);
break;
}
case "3": {
const a = Number(await getinput("Masukkan angka pertama"));
const b = Number(await getinput("Masukkan angka kedua"));
console.log(`Hasil: ${a} * ${b} = ${a * b}`);
break;
}
case "4": {
const a = Number(await getinput("Masukkan angka pertama"));
const b = Number(await getinput("Masukkan angka kedua"));
if (b === 0) {
console.log("❌ Tidak bisa dibagi 0");
} else {
console.log(`Hasil: ${a} / ${b} = ${a / b}`);
}
break;
}
case "0":
jalan = false;
console.log("Terima kasih, program selesai.");
break;
default:
console.log("Pilihan tidak valid, coba lagi!");
}
}
await closeInput();
this ui for file calculator.js

3. Running code
For running code we typing this
node calculator.js
then appear this

4. Closing
thanks for watching , and see you next time…



