PDA

View Full Version : Error message for function if no result found



mrpotatoe
Feb 7th, 2011, 10:46 AM
First post - spent the whole afternoon trying to figure it out and have hit a wall.

I'm trying to check a database of lotto numbers against 3 different values and have gotten it that to work. What I need is to give a feedback message if no match is found once the submit button is clicked.


function winCheck() {
var grandPrize = $('#grandPrize').val();
var otherPrize = $('#otherPrize').val();
var addPrize = $('#addPrize').val();
var resultrange = $('#resultrange').val();
db.transaction(
function(transaction) {
transaction.executeSql(
'SELECT * FROM entries WHERE daterange = ? ORDER BY lottonum;',
[resultrange],
function lottoCompare(transaction, result){
for (var i=0; i < result.rows.length; i++) {
var row = result.rows.item(i);
rowData = [row.lottonum];
rowStr = rowData.toString();
//the if else statements should go here.
if (rowStr == grandPrize){
alert('The lotto number ' + grandPrize + ' is a jackpot winner');
}
if (rowStr.slice(-7) == otherPrize.slice(-7)){
alert('The lotto number ' + rowStr + ' is a winner of $40,000');
}
if (rowStr.slice(-6) == otherPrize.slice(-6)){
alert('The lotto number ' + rowStr + ' is a winner of $10,000');
}
if (rowStr.slice(-5) == otherPrize.slice(-5)){
alert('The lotto number ' + rowStr + ' is a winner of $4,000');
}
if (rowStr.slice(-4) == otherPrize.slice(-4)){
alert('The lotto number ' + rowStr + ' is a winner of $1,000');
}
if (rowStr.slice(-3) == otherPrize.slice(-3)){
alert('The lotto number ' + rowStr + ' is a winner of $200');
}
if (rowStr.slice(-3) == addPrize){
alert('The lotto number ' + rowStr + ' is a winner of $200');
}
}
},
errorHandler
);
}
);
return false;
}

Any help is appreciated

Philip M
Feb 7th, 2011, 10:53 AM
if (rowStr.slice(-3) == addPrize){
alert ('The lotto number ' + rowStr + ' is a winner of $200');
}
else {
alert ("Surprise, surprise! You have not won anything!");
return false;
}


Lottery - a tax on people who are bad at math.

mrpotatoe
Feb 7th, 2011, 11:13 AM
[QUOTE=Philip M;1050329]

if (rowStr.slice(-3) == addPrize){
alert ('The lotto number ' + rowStr + ' is a winner of $200');
}
else {
alert ("Surprise, surprise! You have not won anything!");
return false;
}


this works - however it will still show the error even if all of the fields do match a lotto number found in the database. Is there any way to have it skipped if a match is found for any of the inputs?

also if i use this, it will break once it reaches the first match in the database, which is not what i want either - there may be multiple winners in the lottonum column of my database and it needs to check for them all.

if i modify the code to be the following;

[QUOTE=Philip M;1050329]

if (rowStr.slice(-3) == addPrize){
alert ('The lotto number ' + rowStr + ' is a winner of $200');
}
else {alert ("Surprise, surprise! You have not won anything!");
}

it will give the error for each lotto number, which would be annoying since there is 500 or so

thanks for the quick response Phillip!

Philip M
Feb 7th, 2011, 11:20 AM
var prizewon = 0;
///...
if (rowStr.slice(-3) == addPrize){
alert ('The lotto number ' + rowStr + ' is a winner of $200');
prizewon = 1;
}
if (prizewon == 0) {
alert ("Surprise, surprise! You have not won anything!");
return false;
}

mrpotatoe
Feb 7th, 2011, 02:36 PM
var prizewon = 0;
///...
if (rowStr.slice(-3) == addPrize){
alert ('The lotto number ' + rowStr + ' is a winner of $200');
prizewon = 1;
}
if (prizewon == 0) {
alert ("Surprise, surprise! You have not won anything!");
return false;
}

the return false causes the loop to terminate without going through the rest of the numbers in the array.

would a better way be to split each number comparison into its own function and have a seperate error message for each?

thoughts?