1 / 10

BIT116 : Scripting

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.

johnlclark
Télécharger la présentation

BIT116 : Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIT116: Scripting Checking If User Input Is Numeric

  2. 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

  3. Today • Quiz • Detecting numeric input • Q+A on A1, A2, etc

  4. 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)

  5. 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

  6. 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

  7. Checking if a value is a number

  8. 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

  9. 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 

  10. Exercise • Do exercise #1 for this topic • Once you've got that done, pick problems from exercise #2 to do

More Related