如果留空,则从 GCD 计算中删除第三个输入
Remove third input from GCD calculation if left blank
我正在使用以下代码计算用户提供的三个数字的 GCD:
$('#calc').click(function(){
Math.GCD = function(numbers) {
for (var i = 1 ; i < numbers.length ; i++){
numbers[0] = twogcd(numbers[0], numbers[i]);
}
return numbers[0];
function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) {var temp = first; first = second; second = temp;}
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
}
};
Math.LCM = function(first,second) {
return first * (second / this.GCD(first, second));
};
var first = document.getElementById("first").value;
var second = document.getElementById("second").value;
var third = document.getElementById("third").value;
var numbers = [first,second,third];
var GCDresult = Math.GCD(numbers);
alert(GCDresult);
});
与 HTML:
<FORM NAME="calc" method="POST">
<button TYPE="button" ID="calc">CALC</button>
<input type="text" name="stuff[]" class="input-field" id="first"/>
<input type="text" name="stuff[]" class="input-field" id="second"/>
<input type="text" name="stuff[]" class="input-field" id="third"/>
</FORM>
但是,我希望此表单根据用户输入的内容计算 2 和 3 数字的 GCD。因此,如果一个字段留空,null
字段将被忽略,GCD 计算将继续处理两个输入。但是,作为 JavaScript 的新手,我无法弄清楚如何排除空字段。
Fiddle: https://jsfiddle.net/tjj7won4/40/
我该如何修改代码来解决这个问题?
在将值发送到 GCD 函数之前过滤掉 non-numeric 个值,例如
/* Filter out things that don't look like numbers */
var numbers = [first,second,third].filter(function (e, i, a) { /* e = element, i = index, a = array */
return parseFloat(e); /* Is it numeric? */
});
演示
$('#calc').click(function(){
Math.GCD = function(numbers) {
for (var i = 1 ; i < numbers.length ; i++){
numbers[0] = twogcd(numbers[0], numbers[i]);
}
return numbers[0];
function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) {var temp = first; first = second; second = temp;}
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
}
};
Math.LCM = function(first,second) {
return first * (second / this.GCD(first, second));
};
var first = document.getElementById("first").value;
var second = document.getElementById("second").value;
var third = document.getElementById("third").value;
/* Filter out things that don't look like numbers */
var numbers = [first,second,third].filter(function (e, i, a) { /* e = element, i = index, a = array */
return parseFloat(e); /* Is it numeric? */
});
var GCDresult = Math.GCD(numbers);
alert(GCDresult);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<FORM NAME="calc" method="POST">
<button TYPE="button" ID="calc">CALC</button>
<input type="text" name="stuff[]" class="input-field" id="first"/>
<input type="text" name="stuff[]" class="input-field" id="second"/>
<input type="text" name="stuff[]" class="input-field" id="third"/>
</FORM>
更新了您的代码。添加了对第一个输入字段 isEmpty 的检查。如果是,则为其分配下一个非空输入字段的值。
更新了 JS:
$('#calc').click(function(){
Math.GCD = function(numbers) {
for (var i = 1 ; i < numbers.length ; i++){
if(numbers[0]==""){
$.each($('.inputField'),function(){
if($(this).val()!=""){
numbers[0] = $(this).val();
return false;
}
});
}
if(numbers[0]!="" && numbers[i]!=""){
numbers[0] = twogcd(numbers[0], numbers[i])
}
}
return numbers[0];
if (numbers[i] === '' || numbers[i] === 0) {
numbers[0] = twogcd(numbers[0], numbers[i])
};
function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) {var temp = first; first = second; second = temp;}
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
}
};
Math.LCM = function(first,second) {
return first * (second / this.GCD(first, second));
};
var first = $("#first").val();
var second = $("#second").val();
var third = $("#third").val();
var numbers = [first,second,third];
var GCDresult = Math.GCD(numbers);
alert(GCDresult);
});
我正在使用以下代码计算用户提供的三个数字的 GCD:
$('#calc').click(function(){
Math.GCD = function(numbers) {
for (var i = 1 ; i < numbers.length ; i++){
numbers[0] = twogcd(numbers[0], numbers[i]);
}
return numbers[0];
function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) {var temp = first; first = second; second = temp;}
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
}
};
Math.LCM = function(first,second) {
return first * (second / this.GCD(first, second));
};
var first = document.getElementById("first").value;
var second = document.getElementById("second").value;
var third = document.getElementById("third").value;
var numbers = [first,second,third];
var GCDresult = Math.GCD(numbers);
alert(GCDresult);
});
与 HTML:
<FORM NAME="calc" method="POST">
<button TYPE="button" ID="calc">CALC</button>
<input type="text" name="stuff[]" class="input-field" id="first"/>
<input type="text" name="stuff[]" class="input-field" id="second"/>
<input type="text" name="stuff[]" class="input-field" id="third"/>
</FORM>
但是,我希望此表单根据用户输入的内容计算 2 和 3 数字的 GCD。因此,如果一个字段留空,null
字段将被忽略,GCD 计算将继续处理两个输入。但是,作为 JavaScript 的新手,我无法弄清楚如何排除空字段。
Fiddle: https://jsfiddle.net/tjj7won4/40/
我该如何修改代码来解决这个问题?
在将值发送到 GCD 函数之前过滤掉 non-numeric 个值,例如
/* Filter out things that don't look like numbers */
var numbers = [first,second,third].filter(function (e, i, a) { /* e = element, i = index, a = array */
return parseFloat(e); /* Is it numeric? */
});
演示
$('#calc').click(function(){
Math.GCD = function(numbers) {
for (var i = 1 ; i < numbers.length ; i++){
numbers[0] = twogcd(numbers[0], numbers[i]);
}
return numbers[0];
function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) {var temp = first; first = second; second = temp;}
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
}
};
Math.LCM = function(first,second) {
return first * (second / this.GCD(first, second));
};
var first = document.getElementById("first").value;
var second = document.getElementById("second").value;
var third = document.getElementById("third").value;
/* Filter out things that don't look like numbers */
var numbers = [first,second,third].filter(function (e, i, a) { /* e = element, i = index, a = array */
return parseFloat(e); /* Is it numeric? */
});
var GCDresult = Math.GCD(numbers);
alert(GCDresult);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<FORM NAME="calc" method="POST">
<button TYPE="button" ID="calc">CALC</button>
<input type="text" name="stuff[]" class="input-field" id="first"/>
<input type="text" name="stuff[]" class="input-field" id="second"/>
<input type="text" name="stuff[]" class="input-field" id="third"/>
</FORM>
更新了您的代码。添加了对第一个输入字段 isEmpty 的检查。如果是,则为其分配下一个非空输入字段的值。
更新了 JS:
$('#calc').click(function(){
Math.GCD = function(numbers) {
for (var i = 1 ; i < numbers.length ; i++){
if(numbers[0]==""){
$.each($('.inputField'),function(){
if($(this).val()!=""){
numbers[0] = $(this).val();
return false;
}
});
}
if(numbers[0]!="" && numbers[i]!=""){
numbers[0] = twogcd(numbers[0], numbers[i])
}
}
return numbers[0];
if (numbers[i] === '' || numbers[i] === 0) {
numbers[0] = twogcd(numbers[0], numbers[i])
};
function twogcd(first, second) {
if (first < 0) first = -first;
if (second < 0) second = -second;
if (second > first) {var temp = first; first = second; second = temp;}
while (true) {
first %= second;
if (first == 0) return second;
second %= first;
if (second == 0) return first;
}
}
};
Math.LCM = function(first,second) {
return first * (second / this.GCD(first, second));
};
var first = $("#first").val();
var second = $("#second").val();
var third = $("#third").val();
var numbers = [first,second,third];
var GCDresult = Math.GCD(numbers);
alert(GCDresult);
});