衣服大小计算器在js

时间:2018-04-14 17:14:53

标签: javascript

我现在在js中做一个衣服尺寸计算器小部件,现在它使用这个检查输入中的值

var sizes= [];
//Array containing example sizes
sizes[0] = ["XS", "1", "2", "4"];
sizes[1] = ["S", "6", "7", "9"];
sizes[2] = ["M", "11", "13", "15"];
sizes[3] = ["L", "17", "19", "22"];
sizes[4] = ["XL", "24", "27", "28"];
sizes[5] = ["XXL", "30", "32", "34"];
//In this function I receive values for the length, width and sleeve length from an HTML form and compare whether they are smaller than the clothes size
function rightSize(lon, alt, sleeve){
  console.clear();

  for (i=0; i < sizes.length; i++) {
    var rightLon = lon < sizes[i][1];
    var rightAlt = alt < sizes[i][1];
    var rightSleeve = sleeve < sizes[i][1];

    console.log("iteration: " + i +  rightLon);
    console.log("iteration: " + i +  rightAlt);
    console.log("iteration: " + i +  rightSleeve);
    if(rightLon  && rightAlt && rightSleeve ){
        alert("Size: " + sizes[i][0]);
       break;
    }
  }
}

但是当我几乎用每个值调用它时,它返回S作为正确的值。

编辑:这是HTML表单 -

<input type="number" name="largo" id="lon">
<input type="number" name="alto" id="alt">
<input type="number" name="ancho" id="sleeve">
<INPUT TYPE="button" NAME="button" Value="Click"onClick="rightSize(document.getElementById('lon').value, document.getElementById('alt').value, document.getElementById('sleeve').value)">

2 个答案:

答案 0 :(得分:0)

我认为你刚刚在rightSize()函数中输了一个错字。您的旧代码使用sizes[i][1]

使用rightLon,rightAlt和rightSleeve

编辑:此外,您还有for(i=0;i<talles.length;i++)而不是for(var i =0;

var sizes= [];
//Array containing example sizes
sizes[0] = ["XS", "1", "2", "4"];
sizes[1] = ["S", "6", "7", "9"];
sizes[2] = ["M", "11", "13", "15"];
sizes[3] = ["L", "17", "19", "22"];
sizes[4] = ["XL", "24", "27", "28"];
sizes[5] = ["XXL", "30", "32", "34"];
//In this function I receive values for the length, width and sleeve length from an HTML form and compare whether they are smaller than the     clothes size
function rightSize(lon, alt, sleeve){
  console.clear();

  for (var i=0; i < talles.length; i++) {
    var rightLon = lon < sizes[i][1];
    var rightAlt = alt < sizes[i][2];
    var rightSleeve = sleeve < sizes[i][3];

    console.log("iteration: " + i +  rightLon);
    console.log("iteration: " + i +  rightAlt);
    console.log("iteration: " + i +  rightSleeve);
  }
}

请在评论中告诉我这是否仍然无效。

答案 1 :(得分:0)

花了一天半的时间试图解决这个问题。这是“”。我正在将数字与字符串进行比较。