Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- const getCoordinate = (
- x1: number | string,
- y1: number | string,
- x2: number | string,
- y2: number | string
- ) => {
- try {
- let pattern = /\d/g;
- if (
- x1.toString().match(pattern) &&
- x2.toString().match(pattern) &&
- y1.toString().match(pattern) &&
- y2.toString().match(pattern)
- ) {
- let deltaX = +x1 - +x2;
- let deltaY = +y1 - +y2;
- let d = Math.sqrt(deltaX * 2 + deltaY * 2);
- console.log(d);
- } else {
- throw new Error("Input koordinat dalam angka");
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- getCoordinate(3, 4, -4, -5);
- getCoordinate("1", "2", "-1", "-2");
- getCoordinate("x", "2", "-y", "-2");
- getCoordinate(3.2, 4.5, -4.4, -5);
- }
- {
- const sumDigit = (n: any) => {
- try {
- if (n >= 10000) {
- throw new Error("angka harus lebih kecil dari 10000");
- }
- let pattern = /^\d/g;
- let str = String(n);
- let cek = str.match(pattern);
- if (!cek) {
- throw new Error("not a number");
- }
- let digit = str.match(/\d/g);
- if (digit) {
- let value = digit.reduce((a: any, b: any) => a + +b, 0);
- console.log(value);
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- sumDigit(1234);
- sumDigit("1234");
- sumDigit(12345);
- sumDigit("a1234");
- }
- {
- const elapsedTime = (second: string) => {
- try {
- const hari = Math.floor(parseInt(second) / 86400);
- let sisaDetik = parseInt(second) % 86400;
- const jam = Math.floor(sisaDetik / 3600);
- sisaDetik = sisaDetik % 3600;
- const menit = Math.floor(sisaDetik / 60);
- sisaDetik = sisaDetik % 60;
- const hariStr = hari.toString().padStart(2, "0");
- const jamStr = jam.toString().padStart(2, "0");
- const menitStr = menit.toString().padStart(2, "0");
- const detikStr = sisaDetik.toString().padStart(2, "0");
- if (second.search(/[a-zA-Z]/g) > 1) {
- throw new Error(second + " is not number");
- } else {
- let hasil = `${hariStr} Hari ${jamStr} Jam ${menitStr} Menit ${detikStr} Detik`;
- console.log(hasil);
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- elapsedTime("700005A");
- elapsedTime("700005");
- }
- {
- const getRandom = (s: any) => {
- try {
- if (!s.toString().match(/[a-zA-Z]/g)) {
- throw new Error("Value must be character");
- }
- if (s.length < 6) {
- throw new Error("Length must more than 6");
- }
- let password = Math.random().toString(36).slice(2);
- console.log(password);
- } catch (e: any) {
- console.log(e.message);
- }
- };
- getRandom("1234565");
- getRandom(3422);
- getRandom("alvin");
- getRandom("alvinfaiz");
- }
- {
- const convertToRupiah = (value: number, currencyType: string) => {
- try {
- let jpToID = new Intl.NumberFormat("id-ID", {
- style: "currency",
- currency: "IDR",
- }).format(value * 103.99);
- let usdToID = new Intl.NumberFormat("id-ID", {
- style: "currency",
- currency: "IDR",
- }).format(value * 15367.0);
- let eurToID = new Intl.NumberFormat("id-ID", {
- style: "currency",
- currency: "IDR",
- }).format(value * 16401.97);
- let currencyJP = new Intl.NumberFormat("ja-JP", {
- style: "currency",
- currency: "YEN",
- }).format(value);
- let currencyUS = new Intl.NumberFormat("en-US", {
- style: "currency",
- currency: "USD",
- }).format(value);
- let currencyEU = new Intl.NumberFormat("de-DE", {
- style: "currency",
- currency: "EUR",
- }).format(value);
- if (
- currencyType == "yen" ||
- currencyType == "usd" ||
- currencyType == "euro"
- ) {
- if (currencyType == "yen") {
- console.log(`${currencyJP} = ${jpToID}`);
- } else if (currencyType == "usd") {
- console.log(`${currencyUS} = ${usdToID}`);
- } else if (currencyType == "euro") {
- console.log(`${currencyEU} = ${eurToID}`);
- }
- } else {
- throw new Error("No match type currency");
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- convertToRupiah(1000, "yen");
- convertToRupiah(100, "usd");
- convertToRupiah(100, "euro");
- convertToRupiah(100, "");
- }
- {
- const getHeavier = (w1: number, w2: number, w3: number) => {
- try {
- if (w1 >= w2 && w1 >= w3) {
- console.log(w1);
- } else if (w2 >= w1 && w2 >= w3) {
- console.log(w2);
- } else {
- console.log(w3);
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- getHeavier(12, 45, 70);
- }
- {
- const getDays = (month: number | string, year: number | string) => {
- try {
- let checkInt = /\d/g;
- let intMonth = month.toString().match(checkInt);
- let intYear = year.toString().match(checkInt);
- if (intMonth) {
- if (intYear) {
- if (intMonth && intYear) {
- switch (+month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- console.log("This month has 31 days");
- break;
- case 2:
- console.log("This month has 29 days");
- break;
- case 4:
- case 6:
- case 11:
- console.log("This month has 30 days");
- break;
- default:
- console.log("Input invalid");
- }
- } else {
- throw new Error("Bulan dan tahun harus integer");
- }
- } else {
- throw new Error("Tahun harus integer");
- }
- } else {
- throw new Error("Bulan harus integer");
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- getDays("a", 2021);
- getDays("2", "year");
- getDays("m", "year");
- getDays(4, 2000);
- getDays(8, 2021);
- }
- {
- const getDays = (month: number | string, year: number | string) => {
- try {
- let checkInt = /\d/g;
- let intMonth = month.toString().match(checkInt);
- let intYear = year.toString().match(checkInt);
- if (intMonth) {
- if (intYear) {
- if (intMonth && intYear) {
- switch (+month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- console.log("This month has 31 days");
- break;
- case 2:
- console.log("This month has 29 days");
- break;
- case 4:
- case 6:
- case 11:
- console.log("This month has 30 days");
- break;
- default:
- console.log("Input invalid");
- }
- } else {
- throw new Error("Bulan dan tahun harus integer");
- }
- } else {
- throw new Error("Tahun harus integer");
- }
- } else {
- throw new Error("Bulan harus integer");
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- getDays("a", 2021);
- getDays("2", "year");
- getDays("m", "year");
- getDays(4, 2000);
- getDays(8, 2021);
- }
- {
- const getPresentase = (
- income1: number | string,
- income2: number | string
- ) => {
- try {
- let checkInt = /\d/g;
- let intIncome1 = income1.toString().match(checkInt);
- let intIncome2 = income2.toString().match(checkInt);
- if (intIncome1 && intIncome2) {
- let decreaseValue = +income2 - +income1;
- if (income2 < income1) {
- console.log(
- "Total penurunan income " + (decreaseValue / +income1) * 100 + "%"
- );
- } else if (income2 > income1) {
- console.log(
- "Total kenaikan income " + (decreaseValue / +income1) * 100 + "%"
- );
- } else {
- console.log("income sama");
- }
- } else {
- throw new Error("Inputan harus integer");
- }
- } catch (error: any) {
- console.log(error.message);
- }
- };
- getPresentase("abc", "bca");
- getPresentase(600000.0, 750000.0);
- getPresentase(750000.0, 650000.0);
- }
- {
- const totalGaji = (
- gaji1: number | string,
- gaji2: number | string,
- gaji3: number | string
- ) => {
- try {
- let checkChar = /[a-zA-Z]/g;
- let checkGaji1 = gaji1.toString().match(checkChar);
- let checkGaji2 = gaji2.toString().match(checkChar);
- let checkGaji3 = gaji3.toString().match(checkChar);
- if (checkGaji1 || checkGaji2 || checkGaji3) {
- throw new Error("Nominal harus integer");
- }
- let emp1 = +gaji1 + 0.02 * +gaji1;
- let emp2 = +gaji2 + 0.05 * +gaji2;
- let emp3 = +gaji3 + 0.1 * +gaji3;
- let totalGaji = emp1 + emp2 + emp3;
- console.log(totalGaji);
- } catch (error: any) {
- console.log(error.message);
- }
- };
- totalGaji("100", "200", "300a");
- totalGaji(500, 2000, 12000);
- }
Add Comment
Please, Sign In to add comment