Justen Falk's entry

 avatar
unknown
javascript
2 years ago
4.7 kB
4
Indexable
// Write a function which take the container number as
// the input and returns the company which it belongs to
// 1. MCU123458323 -> STARK, 
//    COS123458910 -> COSCO, (3 letters, 9 numbers)
// # 2  LAS1234589101, LAS12345 (LAS letters, 5-11 numbers)-> LASCO,
// #    DAS1234589101  (DAS letters, 3-15 numbers)-> DASCO

// # 3  PAS1234583232, PASDE123458323 (PAS, PASDE, 6-13 numbers)-> PASCO ,
// #    GRE1234583232, GREEK458910, GREE3123213(GRE, GREEK, GREE, 5-13 numbers) -> GREEKO


(() => {
  const idMapping = {
    'MCU': ['MCU'],
    'COS': ['COS'],
    'LAS': ['LAS'],
    'DAS': ['DAS'],
    'PAS': ['PAS', 'PASDE'],
    'GRE': ['GRE', 'GREEK', 'GREE'],
  };

  function findContainerCompany(containerId) {
    let validation = false;
    let companyId = containerId.slice(0,3);
    let serialLengthValidation, serialIndex;

      switch(companyId) {
        case 'MCU':
          validation = containerId.length === 12 && Number(containerId.slice(3));
          if (validation)
            return 'STARK';
          break;

        case 'COS':
          validation = containerId.length === 12 && Number(containerId.slice(3));
          if (validation)
            return 'COSCO';
          break;

        case 'LAS':
          serialLengthValidation = containerId.length >= 8 && containerId.length <= 14;
          serialIndex = Array.from(containerId).findIndex(Number)
          validation = serialLengthValidation && Number(containerId.slice(serialIndex))

          if (validation)
            return 'LASCO';
          break;

          case 'DAS':
            serialLengthValidation = containerId.length >= 6 && containerId.length <= 18;
            serialIndex = Array.from(containerId).findIndex(Number)
            validation = serialLengthValidation && Number(containerId.slice(serialIndex))

            if (validation)
              return 'DASCO';
            break;

          case 'PAS':
            serialIndex = Array.from(containerId).findIndex(Number)
            companyId = containerId.slice(0, serialIndex);

            if (idMapping['PAS'].includes(companyId)) {
              serialLengthValidation = containerId.length >= 9 && containerId.length <= 18;
              validation = serialLengthValidation && Number(containerId.slice(serialIndex))
            }

            if (validation)
              return 'PASCO';
            break;

          case 'GRE':
            serialIndex = Array.from(containerId).findIndex(Number)
            companyId = containerId.slice(0, serialIndex);

            if (idMapping['GRE'].includes(companyId)) {
              serialLengthValidation = containerId.length >= 8 && containerId.length <= 18;
              validation = serialLengthValidation && Number(containerId.slice(serialIndex))
            }

            if (validation)
              return 'GREEKO';
            break;
      }

    return 'n/a'
  }

  const ans1 = findContainerCompany('MCU123458323')
  console.log("🚀 ~ ans1", 'MCU123458323', ans1)
  
  const ans2 = findContainerCompany('COS123458910')
  console.log("🚀 ~ ans2", 'COS123458910', ans2)

  const ans3 = findContainerCompany('foo')
  console.log("🚀 ~ ans3", 'foo', ans3)

  const ans4 = findContainerCompany('MCU1234583233213213123')
  console.log("🚀 ~ ans4", 'MCU1234583233213213123', ans4)

  const ans5 = findContainerCompany('MCU123458MCU')
  console.log("🚀 ~ ans5", 'MCU123458MCU', ans5)

  const ans6 = findContainerCompany('LAS1234589101')
  console.log("🚀 ~ ans6", 'LAS1234589101', ans6)

  const ans7 = findContainerCompany('LAS12345')
  console.log("🚀 ~ ans7", 'LAS12345', ans7)

  const ans8 = findContainerCompany('DAS1234589101')
  console.log("🚀 ~ ans8", 'DAS1234589101', ans8)

  const edge1 = findContainerCompany('DAS123')
  console.log("🚀 ~ edge1", 'DAS123', edge1)

  const edge2 = findContainerCompany('DAS123456789012345')
  console.log("🚀 ~ edge2", 'DAS123456789012345', edge2)

  const edge4 = findContainerCompany('DAS123D123')
  console.log("🚀 ~ edge4", 'DAS123D123', edge4)

  const test1 = findContainerCompany('PAS1234583232')
  console.log("🚀 ~ test1", 'PAS1234583232', test1)

  const test2 = findContainerCompany('PASDE123458323')
  console.log("🚀 ~ test2", 'PASDE123458323', test2)

  const test3 = findContainerCompany('GRE1234583232')
  console.log("🚀 ~ test3", 'GRE1234583232', test3)

  const test4 = findContainerCompany('GREEK458910')
  console.log("🚀 ~ test4", 'GREEK458910', test4)

  const test5 = findContainerCompany('GREE3123213')
  console.log("🚀 ~ test5", 'GREE3123213', test5)

})()
Editor is loading...