const obj = { distance: { unit: 'ft', value: '' }, convert_to: 'cm' };
const inputIn = document.querySelector('.converter__input-in');
const button = document.querySelector('button');
console.log(obj.distance.value);
const convertTo = (obj) => {
const inUnit = obj.distance.unit;
const unit = obj.convert_to;
const inValue = obj.distance.value;
let sum;
const inputOut = document.querySelector('.converter__input-out');
// in meters
if (inUnit === 'cm' && unit === 'm') {
sum = inValue * 0.01;
}
if (inUnit === 'in' && unit === 'm') {
sum = inValue * 0.254;
}
if (inUnit === 'ft' && unit === 'm') {
sum = inValue * 0.3048;
}
// in centimeters
if (inUnit === 'm' && unit === 'cm') {
sum = inValue * 100;
}
if (inUnit === 'in' && unit === 'cm') {
sum = inValue * 2.54;
}
if (inUnit === 'ft' && unit === 'cm') {
sum = inValue * 30.48;
}
// in inches
if (inUnit === 'm' && unit === 'in') {
sum = inValue * 39.3701;
}
if (inUnit === 'cm' && unit === 'in') {
sum = inValue * 0.393701;
}
if (inUnit === 'ft' && unit === 'in') {
sum = inValue * 12;
}
// in foot
if (inUnit === 'm' && unit === 'ft') {
sum = inValue * 3.28084;
}
if (inUnit === 'cm' && unit === 'ft') {
sum = inValue * 0.328084;
}
if (inUnit === 'in' && unit === 'ft') {
sum = inValue * 0.833333;
}
const value = +sum.toFixed(2);
const result = { unit, value };
inputOut.value = value;
return result;
};
inputIn.addEventListener('keyup', (e) => {
e.preventDefault();
obj.distance.value = +inputIn.value;
convertTo(obj);
});
console.log(convertTo(obj));
const unitsParent = document.querySelector('.converter__units');
const unitsItem = document.querySelectorAll('.converter__units-item');
unitsParent.addEventListener('click', (e) => {
const target = e.target;
if (target.classList.contains('converter__units-item')) {
for (let i = 0; i < unitsItem.length; i++) {
unitsItem[i].classList.remove('--active');
}
target.classList.add('--active');
}
});
console.log(convertTo(obj));
const converter = {
"m": {
"cm": {k:0.01 },
"in": {k:0.254 },
"ft": {k:0.3048 },
},
"cm": {
"m": {k:100 },
"in": {k:2.54 },
"ft": {k:30.48 },
},
"in": {
"m": {k:39.3701 },
"cm": {k:0.393701 },
"ft": {k:12 },
},
"ft": {
"m": {k:3.28084 },
"cm": {k:0.328084 },
"in": {k:0.833333 },
},
};
// ...
sum = converter[unit]&&converter[unit][inUnit]&&converter[unit][inUnit]["k"] ? +inValue*converter[unit][inUnit]["k"]: NaN;
const converter = { "m":{ "cm":{k:0.01 }, "in":{k:0.254 }, "ft":{k:0.3048 }, },