Advertisement
Spocoman

Metric Converter(Simple Conditions)

Oct 8th, 2023
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function metricConverter(input) {
  2.     let distance = Number(input[0]);
  3.     let inputMetric = input[1];
  4.     let outputMetric = input[2];
  5.  
  6.     if (inputMetric == "m") {
  7.         distance /= 1000;
  8.     } else if (inputMetric == "cm") {
  9.         distance /= 100000;
  10.     } else if (inputMetric == "mm") {
  11.         distance /= 1000000;
  12.     } else if (inputMetric == "mi") {
  13.         distance /= 0.621371192;
  14.     } else if (inputMetric == "in") {
  15.         distance /= 39370.0787;
  16.     } else if (inputMetric == "ft") {
  17.         distance /= 3280.8399;
  18.     } else if (inputMetric == "yd") {
  19.         distance /= 1093.6133;
  20.     }
  21.  
  22.     if (outputMetric == "m") {
  23.         distance *= 1000;
  24.     } else if (outputMetric == "cm") {
  25.         distance *= 100000;
  26.     } else if (outputMetric == "mm") {
  27.         distance *= 1000000;
  28.     } else if (outputMetric == "mi") {
  29.         distance *= 0.621371192;
  30.     } else if (outputMetric == "in") {
  31.         distance *= 39370.0787;
  32.     } else if (outputMetric == "ft") {
  33.         distance *= 3280.8399;
  34.     } else if (outputMetric == "yd") {
  35.         distance *= 1093.6133;
  36.     }
  37.  
  38.     console.log(distance);
  39.     return;
  40. }
  41.  
  42. Решение с тернарен оператор:
  43.  
  44. function metricConverter(input) {
  45.     let distance = Number(input[0]);
  46.     let inputMetric = input[1];
  47.     let outputMetric = input[2];
  48.  
  49.     distance /=
  50.         inputMetric == "m" ? 1000 :
  51.         inputMetric == "cm" ? 100000 :
  52.         inputMetric == "mm" ? 1000000 :
  53.         inputMetric == "mi" ? 0.621371192 :
  54.         inputMetric == "in" ? 39370.0787 :
  55.         inputMetric == "ft" ? 3280.8399 :
  56.         inputMetric == "yd" ? 1093.6133 : 1;
  57.  
  58.     distance *=
  59.         outputMetric == "m" ? 1000 :
  60.         outputMetric == "cm" ? 100000 :
  61.         outputMetric == "mm" ? 1000000 :
  62.         outputMetric == "mi" ? 0.621371192 :
  63.         outputMetric == "in" ? 39370.0787 :
  64.         outputMetric == "ft" ? 3280.8399 :
  65.         outputMetric == "yd" ? 1093.6133 : 1;
  66.  
  67.     console.log(distance);
  68.     return;
  69. }
  70.  
  71. Решение с речник:
  72.  
  73. function metricConverter(input) {
  74.     let distance = Number(input[0]);
  75.     let inputMetric = input[1];
  76.     let outputMetric = input[2];
  77.  
  78.     let metricsToKM = {
  79.         "km": 1,
  80.         "m": 1000,
  81.         "cm": 100000,
  82.         "mm": 1000000,
  83.         "mi": 0.621371192,
  84.         "in": 39370.0787,
  85.         "ft": 3280.8399,
  86.         "yd": 1093.6133
  87.     };
  88.  
  89.     console.log(distance / metricsToKM[inputMetric] * metricsToKM[outputMetric]);
  90.     return;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement