ผลต่างระหว่างรุ่นของ "01219245/javascript1/tutorial3/2"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย 'Your current JavaScript program in the script block probably look like this: <syntaxhighlight lang="javascript"> var numSticks = 21; ...') |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 5: | แถว 5: | ||
function pickSticks() { | function pickSticks() { | ||
− | var num = $( "#pickNum" ).val(); | + | var num = parseInt( $( "#pickNum" ).val() ); |
− | numSticks | + | numSticks -= num; |
$( "#numSticks" ).text( numSticks ); | $( "#numSticks" ).text( numSticks ); | ||
} | } | ||
แถว 13: | แถว 13: | ||
$("#pickButton").click(pickSticks); | $("#pickButton").click(pickSticks); | ||
}); | }); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Valid number of sticks == | ||
+ | |||
+ | Let's add the code that check that the number of sticks the player want to take is valid, i.e., it is between 1 to 3 and is no larger than the current number of sticks. Let's assume that the input is an integer for now (i.e., that the user won't enter 1.5). If the amount is invalid, let's throw an alert for now. | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
+ | function pickSticks() { | ||
+ | var num = parseInt( $( "#pickNum" ).val() ); | ||
+ | |||
+ | numSticks -= num; | ||
+ | $( "#numSticks" ).text( numSticks ); | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
รุ่นแก้ไขเมื่อ 16:30, 27 มกราคม 2557
Your current JavaScript program in the script block probably look like this:
var numSticks = 21;
function pickSticks() {
var num = parseInt( $( "#pickNum" ).val() );
numSticks -= num;
$( "#numSticks" ).text( numSticks );
}
$(function(){
$("#pickButton").click(pickSticks);
});
เนื้อหา
Valid number of sticks
Let's add the code that check that the number of sticks the player want to take is valid, i.e., it is between 1 to 3 and is no larger than the current number of sticks. Let's assume that the input is an integer for now (i.e., that the user won't enter 1.5). If the amount is invalid, let's throw an alert for now.
function pickSticks() {
var num = parseInt( $( "#pickNum" ).val() );
numSticks -= num;
$( "#numSticks" ).text( numSticks );
}