Have the function checkNums(num1, num2) take both parameters being passed and return the string true if num2 is greater than num1, otherwise return the string false. If the parameter values are equal to each other then return the string -1.
Enter numbers in the fields to determine if second number is greater than the first.
$(document).ready(function() {
function getNums(data) {
var num1 = $(data).find('.number1').val(),
num2 = $(data).find('.number2').val();
if (num1 === '' || num2 === '' || isNaN(num1 * 1) || isNaN(num2 * 1)) {
return false;
} else {
return [num1 * 1, num2 * 1];
}
}
function checkNums(array) {
var num1 = array[0],
num2 = array[1];
if (num2 > num1) {
return true;
} else if (num2 === num1) {
return -1;
} else return false;
}
$('.compare').click(function() {
if (getNums('.data')) {
$('.result p').html('Number 2 is greater than number 1: <span>' + checkNums(getNums('.data')) + '</span>');
} else {
$('.result p').html('Please enter numbers in the fields on the left.');
}
});
});