<input type="text" name="importo_estratto" />
<input type="text" name="numeri_giocati" />
<input type="submit" id="submit" name="submit" />
$(document).ready(function() {
$('input[name=submit]').click(function() {
var importo_estratto = $('input[name=importo_estratto]').val();
var numeri_giocati = $('input[name=numeri_giocati]').val();
// ESTRATTO
if (importo_estratto != '') {
switch(numeri_giocati) {
case 1: paga = 11.23; break;
case 2: paga = 5.62; break;
case 3: paga = 3.74; break;
case 4: paga = 2.81; break;
case 5: paga = 2.25; break;
case 6: paga = 1.87; break;
case 7: paga = 1.60; break;
case 8: paga = 1.40; break;
case 9: paga = 1.25; break;
case 10: paga = 1.12; break;
}
/*if (numeri_giocati == 1) {
var paga = 11.23;
} else if (numeri_giocati == 2) {
var paga = 5.62;
} else if (numeri_giocati == 3) {
var paga = 3.74;
} else if (numeri_giocati == 4) {
var paga = 2.81;
} else if (numeri_giocati == 5) {
var paga = 2.25;
} else if (numeri_giocati == 6) {
var paga = 1.87;
} else if (numeri_giocati == 7) {
var paga = 1.60;
} else if (numeri_giocati == 8) {
var paga = 1.40;
} else if (numeri_giocati == 9) {
var paga = 1.25;
} else if (numeri_giocati == 10) {
var paga = 1.12;
}*/
alert(paga);
} else {
var tot_estratto = 0;
}
return false;
});
});
I need your help.
I don’t know why, but the IF works and the SWITCH not.
The alert outputs “undefined” instead of the value “paga”, but everything seems to be ok for me.
Can someone help me, please?
Thank you very much
Algorithmic Solution
Nobody has noticed that, with the stated values of
paga, an algorithmic solution is available.For numeri_giocati > 0, the value of
pagais given by the formula:Here’s my verification that the algorithm gives the correct values (to 2 decimal places over the range numeri_giocati = 1 to 10).
The algorithm can be implemented with a javascript function
genPaga(seme, n), where seme is the seed (or scalar) value 11.23, and n is a positive integer.The code in the question thus becomes :
The advantage of this approach is that you’re not limited to the range of values hard-coded in a switch/case statement. Say one day you wish to cater for numeri_giocati = 11, 12, 13 etc. The algorithm will handle that automatically. The function
genPaga()is also available to be called elsewhere in the code if necessary.The limitation is that you are bound by the algorithm. If an algorithm were not available (or could not be deduced), then a switch/case may be the only alternative.