-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
66 lines (56 loc) · 2.07 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
console.log("Unit Converter:");
/*
This Function ask for 3 parameters, `distance`, `from`, `to`
distance: the distance in number to convert
Example: 1000, 10, 999, or another number
from: the unit that you want to convert from
Example: m, dm, cm, km, or another unit
to: the unit that you want to convert to
Example: m, dm, cm, km, or another unit
Available Units:
mm ==> milimeters
cm ==> centimeters
dm ==> decimeters
m ==> meters
dam ==> decameter
hm ==> hetometer
km ==> kilometer
*/
let distanceConversion = (distance = 0, from = "", to = "") => {
// First, lets handle some errors
if (isNaN(distance)) {
return "Error: Distance, needs to be a number";
} else if (typeof from !== "string" || typeof to !== "string") {
return "Error: The type of From, and To parameters, has to be string";
} else if (from.length === 0 || to.length === 0) {
return "Error: You need to set a value for From and To";
}
// Now, lets make the logic
// I'm going to store all the distance units in an array
let units = ["mm", "cm", "dm", "m", "dam", "hm", "km"];
// Now, let's know if we have to multiply or divide
if (units.indexOf(from) < units.indexOf(to)) {
// We have to divide
let howManyZeroes = units.indexOf(to) - units.indexOf(from);
let divideBy = "1";
for (let i = 0; i < howManyZeroes; i++) {
divideBy += "0";
}
return Number(`${distance / divideBy}`);
} else if (units.indexOf(from) > units.indexOf(to)) {
// we have to divide
let howManyZeroes = units.indexOf(from) - units.indexOf(to);
let multiplyBy = "1";
for (let i = 0; i < howManyZeroes; i++) {
multiplyBy += "0";
}
return Number(`${distance * multiplyBy}`);
} else {
//we dont need to do nothing, from and to have the same value
return "From and To have to be different";
}
};
const distancia = process.argv[2];
const from = process.argv[3];
const to = process.argv[4];
console.log(distanceConversion(distancia, from, to));