如何在javascript中将6英尺4英寸转换为米

时间:2018-10-18 18:48:05

标签: javascript

这是我的代码,我必须将6英尺4英寸转换为米,并得到不正确的数字。有人可以帮忙吗?

var INCHES_TO_CM = 2.54;
var CM_TO_METERS = 0.01;
var FEET_TO_INCHES = 12;

function start(){
    convertHeightToMeters(6,4);
}

function convertHeightToMeters(feet, inches){
    var meters = FEET_TO_INCHES + INCHES_TO_CM * CM_TO_METERS;
    println(meters);
}

2 个答案:

答案 0 :(得分:0)

function convertHeightToMeters(feet, inches) {
  return ((feet * FEET_TO_INCHES) + inches) * INCHES_TO_CM * CM_TO_METERS;
}

答案 1 :(得分:0)

这是初学者的简单方法。

HTML

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>
  <h1 id"p1">The conversion</h1>
  <script src="myjsfilename.js"></script>
</body>
</html>

javascript

var conversion = function(foot, inch) {
  var inch = inch*2.54;
  var foot = foot*30.48;
  var result = inch + foot;
  result = result/100 + 'm;'
  document.getElementById("p1").innerHTML = result;
};
conversion(6, 4);

以及如何将其变成一个非常简单的网站(https://conversionfttom.000webhostapp.com/)的示例