-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
27 lines (22 loc) · 824 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const fs = require("fs");
const readline = require("readline");
const validator = require("validator");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Masukkan nama anda: ", (nama) => {
rl.question("Masukkan nomor telfon anda: ", (noHP) => {
if (validator.isMobilePhone(noHP, "id-ID")) {
let json = `{ "nama": "${nama}", "noHP": "${noHP}" }`;
if (fs.existsSync("./contacts.json")) {
let data = fs.readFileSync("./contacts.json", "utf-8");
json = `${data.slice(1, data.length - 3)}, ${json}`;
}
fs.writeFileSync("./contacts.json", `[${json}]`);
} else {
console.error("Hanya Menerima Nomor Indonesia!");
}
rl.close();
});
});