[英]Clothes size calculator in js
我現在正在 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)">
我認為您只是在rightSize()
函數中rightSize()
了rightSize()
。 您的舊代碼具有 rightLon、rightAlt 和 rightSleeve,均使用 size sizes[i][1]
編輯:另外,你有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);
}
}
如果這仍然不起作用,請在評論中告訴我。
花了一天半的時間試圖弄清楚。 原來是“”。 我正在將數字與字符串進行比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.