JS长度转换器

JS length converter

开始学习js。我有一个任务 - 制作米、英尺、厘米、英寸的长度转换器。输入数据是一个 json 数组。我尝试编写代码,但结果不正确。请告诉我 - 我做错了什么?

JSON:

var text = [{"unit":"in", "value": 11, "conversionto": "cm"}, 
{"unit": "ft", "value": 22, "conversionto": "cm"}];

读取数组的循环和转换条件:

var result=0;

for (let i = 0; i < text.length; i++) {
    console.log(`Input:`);
    console.log(`${i} unit:${text[i].unit}, value:${text[i].value}, conversionto:${text[i].conversionto}`);
   
    var from=text[i].unit;
    var val=text[i].value;
    var to=text[i].conversionto;
    
//Convert from in to other 

 if(from === "in" && to ==="cm"){
    result = val * 2,54;
 }
 else if(from === "in" && to ==="ft"){
    result = val * 0.083333;
 }
 else if(from === "in" && to ==="m"){
    result= val / 39.370;
 }
 else if(from === "in" && to ==="in"){
    result = from;
 }

//Convert from cm to other 
if(from === "cm" && to ==="in"){
    result = val * 0.39370;
 }
 else if(from === "cm" && to ==="ft"){
    result = val * 0.032808;
 }
 else if(from === "cm" && to ==="m"){
    result= val /100;
 }
 else if(from === "cm" && to ==="cm"){
    result = from;
 }

 //Convert from Meters to other 

 if(from === "m" && to ==="in"){
    result = val * 39.370;
 }
 else if(from === "m" && to ==="ft"){
    result = val * 3.2808;
 }
 else if(from === "m" && to ==="cm"){
    result= val /0.01;
 }
 else if(from === "m" && to === "m"){
    result = from;
 }


//Convert from Feet to other 
if(from === "ft" && to ==="in"){
    result = val *12;
 }
 else if(from === "ft" && to ==="m"){
    result = val /3.2808;
 }
 else if(from === "ft" && to ==="cm"){
    result= val /0.032808;
 }
 else if(from === "ft" && to === "ft"){
    result = from;
 }

console.log(`Output:`, result);

}

结果:

Input: 0 unit:in, value:11, conversionto:cm 
Output: 22 
Input: 1 unit:ft, value:22, conversionto:cm 
Output: 670.568154108754

您在 in 到 cm 的转换中使用了逗号而不是句点。

result = val * 2.54;