BIT116 : Scripting
100 likes | 116 Vues
In this lesson, we will learn how to detect if user input is numeric in JavaScript. We will cover the isNaN() function, parseFloat(), and parseInt(). Practice exercises included.
BIT116 : Scripting
E N D
Presentation Transcript
BIT116: Scripting Checking If User Input Is Numeric
Snomagedon 2019: • Part 1 • (Mon/Tues Feb 4/5) • We’re going to push the schedule forward 1 day up to the midterm • Next Tuesday was a study day, so we’re going to replace it with new material • The midterm still happens on Thursday, Feb 14th • Possible Part 2 • (Mon/Tues Feb 11/12) • If we miss class next week then we’ll push the schedule back, including the day of the midterm • I will announce the push-back on Canvas
Today • Quiz • Detecting numeric input • Q+A on A1, A2, etc
Upcoming • This week: • ASSIGNMENT 1 WAS DUE ON TUESDAY • I haven’t started grading it yet; you can still hand it in, penalty-free • Week after that: • Tuesday: • Radio buttons, check boxes • Exam Review, Exam Q+A time • Assignment 2 is due on this day • YOU ALL WILL GET AN EXTRA REVISION FOR ASSIGNMENT #2 • Thursday • Midterm exam • (more details on the next slides)
Midterm Exam • Thurs, Feb 14th(Unless the Snowmagedon pushes it back a day) • You’ve got the entire class for the exam • You can leave when you’re done • You’re welcome to bring drinks + snacks • Please eat/drink quietly • If you spill something on a computer you’ll be paying for another one. • PLEASE BRING YOUR 3X5 CARD FILLED (BOTH SIDES) WITH HAND-WRITTEN NOTES
Midterm Exam • Paper and pencil ONLY • Practice writing code in, say, NotePad • Practice writing code on paper • Memorize common patterns • “put a textbox and a button on a page, when the user clicks the button get the input and…..” • Detecting non-numeric input (today’s topic) • PLEASE BRING YOUR 3X5 CARD FILLED (BOTH SIDES) WITH HAND-WRITTEN NOTES
JavaScript is loosely typed • 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
How to check if a string (text) is a number • The isNaN() function will tell us if it's NOT a number • NaN = Not ANumber • 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: Convert it to a number using parseFloat/parseInt • Step 2: Check if it's actually a number using isNaN (error & exit if it isn't) • Step 3: Do the math
Exercise • Do exercise #1 for this topic • Once you've got that done, pick problems from exercise #2 to do