Lecture Slides: Checking Whether User Input Is A Number

advertisement
Checking If User Input Is Numeric
Quiz
Detecting numeric input
Finish Prior Lecture
 Y'all work on one of the problems listed
2
Checking if a value is a number
3
 Examples of data types:
 Integer - whole numbers only, but positive, negative, and zero are
allowed
 Floating-point / real - any number (whole or with a decimal point),
positive, negative, zero
 Strings – text ("a string of individual characters")
 Boolean – true or false
 JS will try to convert from one to another in order to help you
out.
 FILE: loose_typing.html
 Note: multiplying by two is great
 Adding 4 isn't great
 Sometimes we need to tell it what to do
4
 The isNaN() function will tell us if it's NOT a number
 NaN = Not A Number
 parseFloat() will then convert it to a real, floating-point number with decimals
 Use parseInt() if you want to drop anything after the decimal point
 Then it's safe to do math on it
 FILE: checking_for_numbers.html
 Step 1: Check if it's actually a number using isNaN (error & exit if it isn't)
 Step 2: Convert it to a number using parseFloat/parseInt
 Step 3: Do the math 
5
 Do exercise #1 for this topic
 Once you've got that done, pick problems from exercise #2 to do
6
Download