Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function metricConverter(input) {
- let distance = Number(input[0]);
- let inputMetric = input[1];
- let outputMetric = input[2];
- if (inputMetric == "m") {
- distance /= 1000;
- } else if (inputMetric == "cm") {
- distance /= 100000;
- } else if (inputMetric == "mm") {
- distance /= 1000000;
- } else if (inputMetric == "mi") {
- distance /= 0.621371192;
- } else if (inputMetric == "in") {
- distance /= 39370.0787;
- } else if (inputMetric == "ft") {
- distance /= 3280.8399;
- } else if (inputMetric == "yd") {
- distance /= 1093.6133;
- }
- if (outputMetric == "m") {
- distance *= 1000;
- } else if (outputMetric == "cm") {
- distance *= 100000;
- } else if (outputMetric == "mm") {
- distance *= 1000000;
- } else if (outputMetric == "mi") {
- distance *= 0.621371192;
- } else if (outputMetric == "in") {
- distance *= 39370.0787;
- } else if (outputMetric == "ft") {
- distance *= 3280.8399;
- } else if (outputMetric == "yd") {
- distance *= 1093.6133;
- }
- console.log(distance);
- return;
- }
- Решение с тернарен оператор:
- function metricConverter(input) {
- let distance = Number(input[0]);
- let inputMetric = input[1];
- let outputMetric = input[2];
- distance /=
- inputMetric == "m" ? 1000 :
- inputMetric == "cm" ? 100000 :
- inputMetric == "mm" ? 1000000 :
- inputMetric == "mi" ? 0.621371192 :
- inputMetric == "in" ? 39370.0787 :
- inputMetric == "ft" ? 3280.8399 :
- inputMetric == "yd" ? 1093.6133 : 1;
- distance *=
- outputMetric == "m" ? 1000 :
- outputMetric == "cm" ? 100000 :
- outputMetric == "mm" ? 1000000 :
- outputMetric == "mi" ? 0.621371192 :
- outputMetric == "in" ? 39370.0787 :
- outputMetric == "ft" ? 3280.8399 :
- outputMetric == "yd" ? 1093.6133 : 1;
- console.log(distance);
- return;
- }
- Решение с речник:
- function metricConverter(input) {
- let distance = Number(input[0]);
- let inputMetric = input[1];
- let outputMetric = input[2];
- let metricsToKM = {
- "km": 1,
- "m": 1000,
- "cm": 100000,
- "mm": 1000000,
- "mi": 0.621371192,
- "in": 39370.0787,
- "ft": 3280.8399,
- "yd": 1093.6133
- };
- console.log(distance / metricsToKM[inputMetric] * metricsToKM[outputMetric]);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement