fork download
  1. // your code goes here
  2.  
  3. let payment = "payment5,200,Paying off: invoiceB";
  4. let invoices = [
  5. "invoiceA,2024-01-01,100",
  6. "invoiceB,2024-02-01,200",
  7. "invoiceC,2023-01-30,1000"
  8. ];
  9.  
  10.  
  11. function solve() {
  12. let payParts = payment.split(",");
  13. let payId = payParts[0];
  14. let amount = payParts[1];
  15. let invoiceId = payParts[2].substr(12);
  16. for (let invoice of invoices) {
  17. if (invoice.split(",")[0] == invoiceId) {
  18. console.log(`${payId} pays off ${amount} for ${invoiceId} due on ${invoice.split(",")[1]}`);
  19. }
  20. }
  21. }
  22.  
  23. let str = "10A13414124218B124564356434567430";
  24. let allowed = ["B12456435"];
  25.  
  26. function solve2() {
  27. let num = 0;
  28. let ids = [];
  29. do {
  30. let temp = parseInt(str[0] + str[1]);
  31. let id = str.substr(2, temp);
  32. ids.push(id);
  33. str = str.substr(2 + temp);
  34. } while (str.length > 1);
  35. console.log(ids);
  36.  
  37. const allowedIds = [];
  38. for (const id of ids) {
  39. // if (allowed.includes(id)) {
  40. // allowedIds.push(id);
  41. // }
  42. for (const allowedId of allowed) {
  43. if (id.startsWith(allowedId)) {
  44. allowedIds.push(id);
  45. }
  46. }
  47. }
  48. console.log(allowedIds);
  49. }
  50.  
  51.  
  52. let str2 = "USD:CAD:DHL:5,USD:GBP:FEDX:10";
  53.  
  54. function solve3(amount, source, target) {
  55. let conversions = {};
  56.  
  57. for (let conversion of str2.split(",")) {
  58. let parts = conversion.split(":");
  59. let src = parts[0];
  60. let tgt = parts[1];
  61. let rate = parts[3];
  62. let post = parts[2];
  63. if (!conversions.hasOwnProperty(src)) {
  64. conversions[src] = {};
  65. }
  66.  
  67. conversions[src][tgt] = { post, rate };
  68.  
  69. if (!conversions.hasOwnProperty(tgt)) {
  70. conversions[tgt] = {};
  71. }
  72. conversions[tgt][src] = { post, rate: 1/rate };
  73.  
  74. // if (src === source && (hop === 1 || tgt === target)) {
  75. // console.log(amount * rate);
  76. // return;
  77. // }
  78.  
  79. // if (src === target && tgt === source) {
  80. // console.log(amount / rate);
  81. // return;
  82. // }
  83. }
  84. // console.log(JSON.stringify(conversions));
  85.  
  86. for (const [ tgt, convObj ] of Object.entries(conversions[source])) {
  87. console.log(`1 ${source} ${JSON.stringify(convObj)}`)
  88. if (tgt === target) {
  89. console.log(JSON.stringify(convObj));
  90. continue;
  91. }
  92. for (const [tgt2, convObj2] of Object.entries(conversions[tgt])) {
  93. if (tgt2 === target) {
  94. console.log(JSON.stringify([convObj, convObj2]));
  95. }
  96. }
  97. }
  98. }
  99.  
  100. solve3(2, "GBP", "CAD");
  101.  
  102.  
  103.  
  104.  
  105.  
Success #stdin #stdout 0.05s 16732KB
stdin
Standard input is empty
stdout
1 GBP {"post":"FEDX","rate":0.1}
[{"post":"FEDX","rate":0.1},{"post":"DHL","rate":"5"}]