用于根据用户 input/max 值交换变量的 JS 函数
JS function for swapping variables depending on user input/max values
我正在尝试做一个简单的成本估算器。有 3 个不同输入的最大值。我想制作一个检查输入值的函数,并尝试确保 z 值始终是三个值中的最低值,只要满足全局约束。
我做了这个代码笔:
http://codepen.io/FredHair/pen/Jdberw?editors=001
这是我的变量交换代码:
function retrVals() {
var coOrds = {}; //empty object
coOrds.x = Number(document.getElementById("x").value); //define each coordinate
coOrds.y = Number(document.getElementById("y").value);
coOrds.z = Number(document.getElementById("z").value);
var min = Math.min(coOrds.x,coOrds.y,coOrds.z); //find the minimum of the three
var temp = coOrds.z; //create a temporary variable to store the current z value
var limitx = 241;
var limity = 191;
var limitz = 331;
if(min === coOrds.z && coOrds.z < limitz && coOrds.x < limitx && coOrds.y < limity){
return [coOrds.x,coOrds.y,coOrds.z]
}
else if(min === coOrds.y && coOrds.z < limity && coOrds.x < limitx && coOrds.y < limitz){
coOrds.z = coOrds.y
coOrds.y = temp
return [coOrds.x,coOrds.y,coOrds.z]
}
else if(min === coOrds.x && coOrds.z < limitx && coOrds.x < limitz && coOrds.y < limity){
coOrds.z = coOrds.y
coOrds.y = temp
return [coOrds.x,coOrds.y,coOrds.z]
}
else{
return [coOrds.x,coOrds.y,coOrds.z]
}
};
约束是:
x = 240;
y = 190;
z = 330;
所以我不想交换会破坏该数量的值,而且我也总是希望尽可能使 z 最低。[=14=]
执行此操作的最佳方法是什么?我的代码是在正确的轨道上还是有更好的方法?
如有任何帮助,我们将不胜感激。
弗雷德
如果我对你的理解正确的话,这个 if 语句应该做你想做的事情:
var temp = coOrds.z; //create a temporary variable to store the current z value
var limitx = 241, limity = 191, limitz = 331;
var xFail, yFail, zFail;
coOrds.x >= limitx ? xFail = true : xFail = false;
coOrds.y >= limity ? yFail = true : yFail = false;
coOrds.z >= limitz ? zFail = true : zFail = false;
if (zFail || (xFail && yFail)) { // z failed OR both x AND y failed, we cant ever handle this because if x failed then x is greater than 240 which wont ever fit into y
// dont accept the input, z-fail can't be handled because if it doesnt fit in z it wont fit in x or y either
} else if (yFail) { // only y failed
if (coOrds.y < limitx && coOrds.x < limity) { // can we swap x and y?
var tmp = coOrds.y;
coOrds.y = coOrds.x;
coOrds.x = tmp;
} else if (coOrds.y < limitz && coOrds.z < limity) { // can we swap z and y?
var tmp = coOrds.y;
coOrds.y = coOrds.z;
coOrds.z = tmp;
}
} else if (xFail) { // only x failed
if (coOrds.x < limity && coOrds.y < limitx) { // can we swap x and y ?
var tmp = coOrds.y;
coOrds.y = coOrds.x;
coOrds.x = tmp;
} else if (coOrds.y < limitz && coOrds.z < limity) { // can we swap z and x?
var tmp = coOrds.x;
coOrds.x = coOrds.z;
coOrds.z = tmp;
}
}
if (min < coOrds.z) { //do we need to swap at all?
coOrds.z = min; // set z to the minimum
if (min == coOrds.x && temp < limitx) { // was x the minimum and was z less than the x limit?
coOrds.x = temp; //x is the new z
} else if (temp < limity) { // y was the minimum, was z less than the y limit?
coOrds.y = temp;
} else { // could not swap, would exceed limits
coOrds.z = temp
}
}
return [coOrds.x,coOrds.y,coOrds.z];
我正在尝试做一个简单的成本估算器。有 3 个不同输入的最大值。我想制作一个检查输入值的函数,并尝试确保 z 值始终是三个值中的最低值,只要满足全局约束。
我做了这个代码笔:
http://codepen.io/FredHair/pen/Jdberw?editors=001
这是我的变量交换代码:
function retrVals() {
var coOrds = {}; //empty object
coOrds.x = Number(document.getElementById("x").value); //define each coordinate
coOrds.y = Number(document.getElementById("y").value);
coOrds.z = Number(document.getElementById("z").value);
var min = Math.min(coOrds.x,coOrds.y,coOrds.z); //find the minimum of the three
var temp = coOrds.z; //create a temporary variable to store the current z value
var limitx = 241;
var limity = 191;
var limitz = 331;
if(min === coOrds.z && coOrds.z < limitz && coOrds.x < limitx && coOrds.y < limity){
return [coOrds.x,coOrds.y,coOrds.z]
}
else if(min === coOrds.y && coOrds.z < limity && coOrds.x < limitx && coOrds.y < limitz){
coOrds.z = coOrds.y
coOrds.y = temp
return [coOrds.x,coOrds.y,coOrds.z]
}
else if(min === coOrds.x && coOrds.z < limitx && coOrds.x < limitz && coOrds.y < limity){
coOrds.z = coOrds.y
coOrds.y = temp
return [coOrds.x,coOrds.y,coOrds.z]
}
else{
return [coOrds.x,coOrds.y,coOrds.z]
}
};
约束是:
x = 240;
y = 190;
z = 330;
所以我不想交换会破坏该数量的值,而且我也总是希望尽可能使 z 最低。[=14=]
执行此操作的最佳方法是什么?我的代码是在正确的轨道上还是有更好的方法?
如有任何帮助,我们将不胜感激。
弗雷德
如果我对你的理解正确的话,这个 if 语句应该做你想做的事情:
var temp = coOrds.z; //create a temporary variable to store the current z value
var limitx = 241, limity = 191, limitz = 331;
var xFail, yFail, zFail;
coOrds.x >= limitx ? xFail = true : xFail = false;
coOrds.y >= limity ? yFail = true : yFail = false;
coOrds.z >= limitz ? zFail = true : zFail = false;
if (zFail || (xFail && yFail)) { // z failed OR both x AND y failed, we cant ever handle this because if x failed then x is greater than 240 which wont ever fit into y
// dont accept the input, z-fail can't be handled because if it doesnt fit in z it wont fit in x or y either
} else if (yFail) { // only y failed
if (coOrds.y < limitx && coOrds.x < limity) { // can we swap x and y?
var tmp = coOrds.y;
coOrds.y = coOrds.x;
coOrds.x = tmp;
} else if (coOrds.y < limitz && coOrds.z < limity) { // can we swap z and y?
var tmp = coOrds.y;
coOrds.y = coOrds.z;
coOrds.z = tmp;
}
} else if (xFail) { // only x failed
if (coOrds.x < limity && coOrds.y < limitx) { // can we swap x and y ?
var tmp = coOrds.y;
coOrds.y = coOrds.x;
coOrds.x = tmp;
} else if (coOrds.y < limitz && coOrds.z < limity) { // can we swap z and x?
var tmp = coOrds.x;
coOrds.x = coOrds.z;
coOrds.z = tmp;
}
}
if (min < coOrds.z) { //do we need to swap at all?
coOrds.z = min; // set z to the minimum
if (min == coOrds.x && temp < limitx) { // was x the minimum and was z less than the x limit?
coOrds.x = temp; //x is the new z
} else if (temp < limity) { // y was the minimum, was z less than the y limit?
coOrds.y = temp;
} else { // could not swap, would exceed limits
coOrds.z = temp
}
}
return [coOrds.x,coOrds.y,coOrds.z];