alvinfnaldi

Typescript Regex

Sep 21st, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.   const getCoordinate = (
  3.     x1: number | string,
  4.     y1: number | string,
  5.     x2: number | string,
  6.     y2: number | string
  7.   ) => {
  8.     try {
  9.       let pattern = /\d/g;
  10.       if (
  11.         x1.toString().match(pattern) &&
  12.         x2.toString().match(pattern) &&
  13.         y1.toString().match(pattern) &&
  14.         y2.toString().match(pattern)
  15.       ) {
  16.         let deltaX = +x1 - +x2;
  17.         let deltaY = +y1 - +y2;
  18.         let d = Math.sqrt(deltaX * 2 + deltaY * 2);
  19.         console.log(d);
  20.       } else {
  21.         throw new Error("Input koordinat dalam angka");
  22.       }
  23.     } catch (error: any) {
  24.       console.log(error.message);
  25.     }
  26.   };
  27.  
  28.   getCoordinate(3, 4, -4, -5);
  29.   getCoordinate("1", "2", "-1", "-2");
  30.   getCoordinate("x", "2", "-y", "-2");
  31.   getCoordinate(3.2, 4.5, -4.4, -5);
  32. }
  33.  
  34. {
  35.   const sumDigit = (n: any) => {
  36.     try {
  37.       if (n >= 10000) {
  38.         throw new Error("angka harus lebih kecil dari 10000");
  39.       }
  40.  
  41.       let pattern = /^\d/g;
  42.       let str = String(n);
  43.       let cek = str.match(pattern);
  44.  
  45.       if (!cek) {
  46.         throw new Error("not a number");
  47.       }
  48.  
  49.       let digit = str.match(/\d/g);
  50.  
  51.       if (digit) {
  52.         let value = digit.reduce((a: any, b: any) => a + +b, 0);
  53.         console.log(value);
  54.       }
  55.     } catch (error: any) {
  56.       console.log(error.message);
  57.     }
  58.   };
  59.   sumDigit(1234);
  60.   sumDigit("1234");
  61.   sumDigit(12345);
  62.   sumDigit("a1234");
  63. }
  64.  
  65. {
  66.   const elapsedTime = (second: string) => {
  67.     try {
  68.       const hari = Math.floor(parseInt(second) / 86400);
  69.       let sisaDetik = parseInt(second) % 86400;
  70.       const jam = Math.floor(sisaDetik / 3600);
  71.       sisaDetik = sisaDetik % 3600;
  72.       const menit = Math.floor(sisaDetik / 60);
  73.       sisaDetik = sisaDetik % 60;
  74.  
  75.       const hariStr = hari.toString().padStart(2, "0");
  76.       const jamStr = jam.toString().padStart(2, "0");
  77.       const menitStr = menit.toString().padStart(2, "0");
  78.       const detikStr = sisaDetik.toString().padStart(2, "0");
  79.       if (second.search(/[a-zA-Z]/g) > 1) {
  80.         throw new Error(second + " is not number");
  81.       } else {
  82.         let hasil = `${hariStr} Hari ${jamStr} Jam ${menitStr} Menit ${detikStr} Detik`;
  83.         console.log(hasil);
  84.       }
  85.     } catch (error: any) {
  86.       console.log(error.message);
  87.     }
  88.   };
  89.   elapsedTime("700005A");
  90.   elapsedTime("700005");
  91. }
  92.  
  93. {
  94.   const getRandom = (s: any) => {
  95.     try {
  96.       if (!s.toString().match(/[a-zA-Z]/g)) {
  97.         throw new Error("Value must be character");
  98.       }
  99.       if (s.length < 6) {
  100.         throw new Error("Length must more than 6");
  101.       }
  102.  
  103.       let password = Math.random().toString(36).slice(2);
  104.       console.log(password);
  105.     } catch (e: any) {
  106.       console.log(e.message);
  107.     }
  108.   };
  109.   getRandom("1234565");
  110.   getRandom(3422);
  111.   getRandom("alvin");
  112.   getRandom("alvinfaiz");
  113. }
  114.  
  115. {
  116.   const convertToRupiah = (value: number, currencyType: string) => {
  117.     try {
  118.       let jpToID = new Intl.NumberFormat("id-ID", {
  119.         style: "currency",
  120.         currency: "IDR",
  121.       }).format(value * 103.99);
  122.  
  123.       let usdToID = new Intl.NumberFormat("id-ID", {
  124.         style: "currency",
  125.         currency: "IDR",
  126.       }).format(value * 15367.0);
  127.  
  128.       let eurToID = new Intl.NumberFormat("id-ID", {
  129.         style: "currency",
  130.         currency: "IDR",
  131.       }).format(value * 16401.97);
  132.  
  133.       let currencyJP = new Intl.NumberFormat("ja-JP", {
  134.         style: "currency",
  135.         currency: "YEN",
  136.       }).format(value);
  137.  
  138.       let currencyUS = new Intl.NumberFormat("en-US", {
  139.         style: "currency",
  140.         currency: "USD",
  141.       }).format(value);
  142.  
  143.       let currencyEU = new Intl.NumberFormat("de-DE", {
  144.         style: "currency",
  145.         currency: "EUR",
  146.       }).format(value);
  147.  
  148.       if (
  149.         currencyType == "yen" ||
  150.         currencyType == "usd" ||
  151.         currencyType == "euro"
  152.       ) {
  153.         if (currencyType == "yen") {
  154.           console.log(`${currencyJP} = ${jpToID}`);
  155.         } else if (currencyType == "usd") {
  156.           console.log(`${currencyUS} = ${usdToID}`);
  157.         } else if (currencyType == "euro") {
  158.           console.log(`${currencyEU} = ${eurToID}`);
  159.         }
  160.       } else {
  161.         throw new Error("No match type currency");
  162.       }
  163.     } catch (error: any) {
  164.       console.log(error.message);
  165.     }
  166.   };
  167.   convertToRupiah(1000, "yen");
  168.   convertToRupiah(100, "usd");
  169.   convertToRupiah(100, "euro");
  170.   convertToRupiah(100, "");
  171. }
  172.  
  173. {
  174.   const getHeavier = (w1: number, w2: number, w3: number) => {
  175.     try {
  176.       if (w1 >= w2 && w1 >= w3) {
  177.         console.log(w1);
  178.       } else if (w2 >= w1 && w2 >= w3) {
  179.         console.log(w2);
  180.       } else {
  181.         console.log(w3);
  182.       }
  183.     } catch (error: any) {
  184.       console.log(error.message);
  185.     }
  186.   };
  187.   getHeavier(12, 45, 70);
  188. }
  189.  
  190. {
  191.   const getDays = (month: number | string, year: number | string) => {
  192.     try {
  193.       let checkInt = /\d/g;
  194.       let intMonth = month.toString().match(checkInt);
  195.       let intYear = year.toString().match(checkInt);
  196.  
  197.       if (intMonth) {
  198.         if (intYear) {
  199.           if (intMonth && intYear) {
  200.             switch (+month) {
  201.               case 1:
  202.               case 3:
  203.               case 5:
  204.               case 7:
  205.               case 8:
  206.               case 10:
  207.               case 12:
  208.                 console.log("This month has 31 days");
  209.                 break;
  210.               case 2:
  211.                 console.log("This month has 29 days");
  212.                 break;
  213.               case 4:
  214.               case 6:
  215.               case 11:
  216.                 console.log("This month has 30 days");
  217.                 break;
  218.               default:
  219.                 console.log("Input invalid");
  220.             }
  221.           } else {
  222.             throw new Error("Bulan dan tahun harus integer");
  223.           }
  224.         } else {
  225.           throw new Error("Tahun harus integer");
  226.         }
  227.       } else {
  228.         throw new Error("Bulan harus integer");
  229.       }
  230.     } catch (error: any) {
  231.       console.log(error.message);
  232.     }
  233.   };
  234.   getDays("a", 2021);
  235.   getDays("2", "year");
  236.   getDays("m", "year");
  237.   getDays(4, 2000);
  238.   getDays(8, 2021);
  239. }
  240.  
  241. {
  242.   const getDays = (month: number | string, year: number | string) => {
  243.     try {
  244.       let checkInt = /\d/g;
  245.       let intMonth = month.toString().match(checkInt);
  246.       let intYear = year.toString().match(checkInt);
  247.  
  248.       if (intMonth) {
  249.         if (intYear) {
  250.           if (intMonth && intYear) {
  251.             switch (+month) {
  252.               case 1:
  253.               case 3:
  254.               case 5:
  255.               case 7:
  256.               case 8:
  257.               case 10:
  258.               case 12:
  259.                 console.log("This month has 31 days");
  260.                 break;
  261.               case 2:
  262.                 console.log("This month has 29 days");
  263.                 break;
  264.               case 4:
  265.               case 6:
  266.               case 11:
  267.                 console.log("This month has 30 days");
  268.                 break;
  269.               default:
  270.                 console.log("Input invalid");
  271.             }
  272.           } else {
  273.             throw new Error("Bulan dan tahun harus integer");
  274.           }
  275.         } else {
  276.           throw new Error("Tahun harus integer");
  277.         }
  278.       } else {
  279.         throw new Error("Bulan harus integer");
  280.       }
  281.     } catch (error: any) {
  282.       console.log(error.message);
  283.     }
  284.   };
  285.   getDays("a", 2021);
  286.   getDays("2", "year");
  287.   getDays("m", "year");
  288.   getDays(4, 2000);
  289.   getDays(8, 2021);
  290. }
  291.  
  292. {
  293.   const getPresentase = (
  294.     income1: number | string,
  295.     income2: number | string
  296.   ) => {
  297.     try {
  298.       let checkInt = /\d/g;
  299.       let intIncome1 = income1.toString().match(checkInt);
  300.       let intIncome2 = income2.toString().match(checkInt);
  301.  
  302.       if (intIncome1 && intIncome2) {
  303.         let decreaseValue = +income2 - +income1;
  304.  
  305.         if (income2 < income1) {
  306.           console.log(
  307.             "Total penurunan income " + (decreaseValue / +income1) * 100 + "%"
  308.           );
  309.         } else if (income2 > income1) {
  310.           console.log(
  311.             "Total kenaikan income " + (decreaseValue / +income1) * 100 + "%"
  312.           );
  313.         } else {
  314.           console.log("income sama");
  315.         }
  316.       } else {
  317.         throw new Error("Inputan harus integer");
  318.       }
  319.     } catch (error: any) {
  320.       console.log(error.message);
  321.     }
  322.   };
  323.  
  324.   getPresentase("abc", "bca");
  325.   getPresentase(600000.0, 750000.0);
  326.   getPresentase(750000.0, 650000.0);
  327. }
  328.  
  329. {
  330.   const totalGaji = (
  331.     gaji1: number | string,
  332.     gaji2: number | string,
  333.     gaji3: number | string
  334.   ) => {
  335.     try {
  336.       let checkChar = /[a-zA-Z]/g;
  337.  
  338.       let checkGaji1 = gaji1.toString().match(checkChar);
  339.       let checkGaji2 = gaji2.toString().match(checkChar);
  340.       let checkGaji3 = gaji3.toString().match(checkChar);
  341.  
  342.       if (checkGaji1 || checkGaji2 || checkGaji3) {
  343.         throw new Error("Nominal harus integer");
  344.       }
  345.       let emp1 = +gaji1 + 0.02 * +gaji1;
  346.       let emp2 = +gaji2 + 0.05 * +gaji2;
  347.       let emp3 = +gaji3 + 0.1 * +gaji3;
  348.  
  349.       let totalGaji = emp1 + emp2 + emp3;
  350.       console.log(totalGaji);
  351.     } catch (error: any) {
  352.       console.log(error.message);
  353.     }
  354.   };
  355.  
  356.   totalGaji("100", "200", "300a");
  357.   totalGaji(500, 2000, 12000);
  358. }
Add Comment
Please, Sign In to add comment