Reputation: 251
I am using the following code to calculate the GCD of three numbers supplied by a user:
$('#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);
});
with 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>
However, I would like for this form to calculate the GCD of both 2 and 3 numbers depending on what the user inputs. Thus, if a field is left blank, the null
field will be ignored and the GCD calculation will proceed with the two inputs. However, I cannot, being relatively new to JavaScript, figure out how to exclude an empty field.
Fiddle: https://jsfiddle.net/tjj7won4/40/
How might I alter the code to account for this?
Upvotes: 0
Views: 70
Reputation: 3265
Filter out non-numeric values before sending it to the GCD function, something like
/* 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? */
});
Demo
$('#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>
Upvotes: 0
Reputation: 26
Updated your code. Added a check for first input field isEmpty. If yes then assigned it a value of next nonEmpty input field.
updated 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);
});
Upvotes: 1