JavaScript – If/Else contd

Slides:



Advertisements
Similar presentations
Modifying existing content Adding/Removing content on a page using jQuery.
Advertisements

Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
CIS101 Introduction to Computing Week 12 Spring 2004.
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
JavaScript Form Validation
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Conditional Execution
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
COMP Loop Statements Yi Hong May 21, 2015.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path.
Programming for the Web Assignment One Dónal Mulligan BSc MA
COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
JavaScript: Conditionals contd.
BIT116: Scripting Lecture 05
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
JavaScript Controlling the flow of your programs with ‘if’ statements
Precedence Operators Error Types
How To Register & Order Meals on Meal Selector
How to Calculate Your Body Mass Index (BMI)
Moving away from alert() Using innerHTML Using an empty div section
GCSE COMPUTER SCIENCE Practical Programming using Python
Chapter 3 Control Statements
IT 130 – Internet and the Web - Yosef Mendelsohn
REPETITION CONTROL STRUCTURE
Checkboxes, Select boxes, Radio buttons,
Topic: Conditional Statements – Part 1
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Building Java Programs Chapter 4
Retrieving information from forms
Programming the Web using XHTML and JavaScript
Chapter 6: Conditional Statements and Loops
Web Programming– UFCFB Lecture 17
How to Use Members Area of The Ninety-Nines Website
Lecture 4: Conditionals
MIS 3200 – Unit 4 Complex Conditional Statements else if switch.
DAYS OF THE WEEK.
JavaScript Selection Statement Creating Array
Lesson 8: Boolean Expressions and "if" Statements
Conditions and Ifs BIS1523 – Lecture 8.
During the last lecture we had a discussion on Data Types, Variables & Operators
T. Jumana Abu Shmais – AOU - Riyadh
Programming in JavaScript
Building Java Programs
Conditional Execution
Relational & Logical Operators, if and switch Statements
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Programming in JavaScript
Week 1-6.
BIT116: Scripting Lecture 6 Part 1
Building Java Programs
Building Java Programs
Contact
Welcome to Math 1! Can you guess what team I cheer for and where I
Factoring if/else code
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
Retrieving information from forms
Checkboxes, Select boxes, Radio buttons
Presentation transcript:

JavaScript – If/Else contd If, else-if Limiting free-text entry

Learning Objectives By the end of this lecture, you should be able to: Describe how the JavaScript flow operates with if and else-if statements Provide at least two reasons why it’s a good idea to limit free-text entry by users Describe and apply some techniques for limiting free-text entry

Cascading if statements: if, and else-if The if-else examples we have been using so far have involved only two possible scenarios: a situation that has only one of two possible results. For example: Pay the meter or not? Pay overtime or not? Qualified to vote or not? In the real world, we frequently have multiple possible scenarios we want to evaluate for. Example – Assigning a grade: if the percent grade is above 90, assign ‘A’ if grade is above 80 and less than 90, assign ‘B’ if grade is above 70 and less than 80, assign ‘C’ if grade is above 60 and less than 70, assign ‘D’ if < 60 assign ‘F’ 3

var percent, letterGrade; percent = document.getElementById('txtPctg').value; percent = parseFloat(percent); if (percent >= 90) { letterGrade = "A"; } else if (percent >=80) letterGrade = "B"; else if (percent >= 70) letterGrade = "C"; else if (percent >=60) letterGrade = “D”; else letterGrade = “F”; Remainder of the program continues…. How it works: Note that there is a space between the word ‘else’ and the word ‘if’: else if As soon as any one of the conditionals is evaluated as being TRUE, the code will enter the appropriate block (delineated by the braces). Once any one of the blocks has been executed, the program will skip ALL of the remaining else if statements.

Should you include an ‘else’ block? It is usually a good idea to have an ‘else’ at the end of a series of ‘else-if’ statements. If all of the conditionals are false, then the ‘else’ statement will be executed. However, the moment any one of the condititional's blocks is executed, flow will jump to the end of the entire block of if-else statements (as discussed previously). This includes the ‘else’ block. var percent, letterGrade; percent = document.form.txtPercent.value; percent = parseInt(percent); if (percent >= 90) { letterGrade = "A"; } else if (percent >=80) letterGrade = "B"; else if (percent >= 70) letterGrade = "C"; else if (percent >=60) letterGrade = “D”; else letterGrade = “F”; Remainder of the program continues….

Avoiding text entry by users When visitors to your page enter data on their own, there is tremendous opportunity for bugs – and even hacking. For example, hackers have successfully entered JavaScript code into text fields and used that code to gain entry to “secure” networks! On a more benign level, imagine you want the user to tell you the day of the week (recall our parking meter example). Will they enter Sunday, sunday, SUNDAY? What about typos? Ssunday, Sunda, etc, etc. This is why web designers will, whenever possible take the option for text entry out of the user’s hands. They can do so by requiring the user to use select boxes (e.g. for day of the week), check boxes (I agree/Disagree), Radio Buttons (rate from 1 to 10), Calendar widgets (for dates), and so on. Generally speaking, you should only allow the user to enter text where it is absolutely required. Some examples: Name Email Address Address Phone number

Eliminating free-text entry Suppose you wanted to ask the user their favorite Chicago sports team. Will they enter: Cubs, cubs, Chicago cubs, Sox, White Sox, White-Sox, CWS, Hawks, Blackhawks, BLACKHAWKS!, etc, etc, etc. In other words, we have absolutely no idea what they will enter. Our solution to this problem is to take away the user’s ability to write any free text at all. Instead, we use form elements such as radio buttons, or, in this case, a select box. In general it is a good idea to prevent the user from entering free text into a form whenever possible. Review select_box_js.htm

Exercise See the file bmi_calc.htm This page calculates the user’s body mass index based on their height and weight. Modify this program to also output the patient’s classification based on the following criteria: BMI < 18.5  Output: “Underweight” BMI 18.5 – 24.99  Output: “Normal Weight” BMI 25 – 29.99  Output: “Overweight” BMI >= 30  Output: “Obese” So, in addition to outputting the patient’s numeric BMI, the program should also output their classification. Once you have tried it, you can look at my version: bmi_calc_with_classification.htm

Practice Here are a couple of suggested exercises.

Exercise #1: Prompt the user for the day of the week again by using a select box.  If they choose Saturday or Sunday (you MUST use the logical or “||” to do this) write an alert box that says:  “Have a great weekend!” If they choose Monday or Tuesday (you MUST use the logical or “||” to do this) alert: “Hope you had a good weekend” Wednesday alert: “Hump Day!” Thursday alert: “Almost there…” Friday, alert: “TGIF!”

Exercise #2: Prompt the user for their income over the past year.  Then have a button that says “Calculate my taxes”.  Calculate their tax based on the following criteria: If the user has an income less than $30,000, their tax rate should be 0%.  If their income is between $30-49,999 their rate should be 25%.  If their income is between $50-99,999 use 35%.  $100,000 and more use 40%.  Output a result that says something like: For an income of $64,000, you must pay $22,400 in taxes.