JavaScript, continued.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
CSCI 116 Decisions & Validation. Overview Constants Boolean conditions Decision logic Form validation 2.
JavaScript Lesson 2 TBE 540 F. Fisher. Prerequisites  Before beginning this lesson, the learner must be able to… Create and edit a web page using a text.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CIS101 Introduction to Computing Week 12 Spring 2004.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Computers and Scientific Thinking David Reed, Creighton University JavaScript and User Interaction 1.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Unit 3 Day 6 FOCS – Web Design. Journal Unit #3 Entry #4 Write the source code that would make the following display on a rendered page: Shopping List.
Using Client-Side Scripts to Enhance Web Applications 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Conditions in Java. First…Boolean Operators A boolean data type is always true or false. Boolean operators always return true or false For example: (x.
22/11/ Selection If selection construct.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
31/01/ Selection If selection construct.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Decision Structures. Spacecraft Divine Vessel V will orbits earth 14 times in 21 hours First manned spacecraft of China, only the third country that can.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
JavaScript, Third Edition 1 SELECTION LIST Demo. JavaScript, Third Edition 2 Description This web page will edit the slection to ensure an option was.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
BIT116: Scripting Lecture 05
CGS 3066: Web Programming and Design Spring 2017
CHAPTER 10 JAVA SCRIPT.
Chapter 5 The if Statement
Chapter 4 The If…Then Statement
Line Continuation, Output Formatting, and Decision Structures
JavaScript.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Chapter 4: Control Structures
Programming the Web using XHTML and JavaScript
JavaScript, continued.
Chapter 4: Making Decisions.
JavaScript Functions.
Chapter 6: Conditional Statements and Loops
Javascript Conditionals.
JavaScript conditional
MIS 3200 – Unit 4 Complex Conditional Statements else if switch.
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Javascript Game Assessment
Selection Statements Chapter 4 Attaway MATLAB 4E.
T. Jumana Abu Shmais – AOU - Riyadh
If selection construct
Introduction to Primitive Data types
If selection construct
Relational & Logical Operators, if and switch Statements
JavaScript Part 2.
Conditional Logic Presentation Name Course Name
JavaScript conditional
The Selection Structure
Selection Statements Chapter 4 Attaway MATLAB 5E.
Web Programming and Design
CGS 3066: Web Programming and Design Spring 2016
For the CIS17 course..
Lecture 9: JavaScript Syntax
Intro to Programming (in JavaScript)
Introduction to Primitive Data types
Decision Statements.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

JavaScript, continued

Conditionals

Principles Each decision breaks the space in 2 pieces: either the test is true or false: NO PARTIAL CREDIT When you get to the last case, you don’t need a test: EVERYTHING ELSE IS HERE

Multiple Choices: 1 choice if Use when you sometimes take an action. If its not true, you take no action. Example: You need the difference between two numbers, but don’t care which is bigger if (test) {action} function getDiff (val1, val2) { var diff = val1 – val2; if (diff < 0) { diff = -diff; } return(“The difference is “+diff);

Multiple Choices: 2 choices if - else Use when there are exactly 2 options and you are going to take different actions depending on which is true. Example: Whether it is going to rain or snow depends on the temperature. if (test) {true action} else {false action} function weather (temp) { var precip; if (temp < 32) { precip = “snow”; } else { precip = “rain”; } return(“It is going to “+precip);

Multiple Choices: 3 choices if – else if - else Use when there are more than 2 choices and there are different actions in each case. Example: Determine if it is a win, loss, tie. if (test1) {test1 true action} else if (test2) {test2 true action} else {test1 and test2 false action} function gameOutcome (homeScore, visScore) { var result; if (homeScore > visScore) { result = “win”; } else if (homeScore < visScore) { result = “loss”; ) else { result = “tie”; ) return(“It was a “+result+” for the home team”); }

Build a decision tree Test in a diamond Left if it true, right if it is false If you need to make more decisions, repeat All cases are at the bottom: these are where the actions go Diamond = test Left = “then” clause Right = else clause. If it needs another decision: else if Otherwise: just else

Random Selections Identify what your options are Strawberries, Blueberries, Raspberries Identify what random values will give you that choice 0-.5: strawberries .5-.75 blueberries .75-1 raspberries These create your decisions and actions Now build a decision tree

Decision Tree (one of several) grade < 60 F grade < 70 D grade < 80 C grade < 90 B A

Order of the Decisions Matter grade < 60 0-59 F 60-100 grade < 70 60-69 D 70-100 grade < 80 70-79 C 80-100 grade < 90 80-89 B 90-100 A

Order of the Decisions Matter grade < 90 0-89 90-100 grade < 80 NEVER TRUE

Fix the fiddle Found under resources Fix it so that all responses are equally likely

Creating HTML

What to put in innerHTML? It MUST be PROPER HTML If you would have needed quotation marks if you were typing the HTML, you need to create those quotation marks is JavaScript Example: I want to produce the HTML tag <img src=“cow.jpg” alt=“cows in the field”> To create that string in JavaScript: var imgTag = ‘<img src=“cow.jpg” alt=“cows in the field”>’;

Building the img tag from arrays <img src=“cow.jpg” alt=“cows in the field”> Two arrays: var srcNames = [“cow.jpg”, “pig.jpg”, “dog.jpg”]; var altText = [“cows in the field”, “pig eating”, “dog playing catch”]; Note that the quotation marks here say that these are strings. They are NOT part of the string itself. To put the img tag together: var imgTag = ‘<img src=“’+srcNames[0]+’” alt=“’+altText[0]+’”>’;

OR, I can create a single array with the entire img tag all built One array: var imgTags = [ ‘<img src=“cow.jpg” alt=“cows in the field”>’, ‘<img src=“pig.jpg” alt=“pig eatin”>’, ‘<img src=“dog.jpg” alt=“dog playing catch”>’]; var imgTag = imgTags[0];

Create a Web Page User types a question and clicks the button Response is to be in a paragraph It is to appear as The magic ball says “response here” You are to display the quotation marks and the actual response is to be in italics, using the em tag

Testing Multiple Conditions

Operations on Booleans AND OR Are all of them true? && Is it a big dog? (size==“big” && pet==“dog”) Number between 5 and 10 (num>=5 && num<=10) Are any of them true? || Is it either blue or black? (color==“blue” || color==“black”) Number less than 10 or greater than 20 (num<10 || num>20)

Is greater than or equal to Operators Conditional Operator Meaning && and || or ! not Operator Meaning == Is equal to != Is not equal > Is greater than < Is less than >= Is greater than or equal to <= Is less than or equal to Logical

Add a greeting based on the time Extend the greeting to say one of the following: 8 pm-5 am Good Night (THIS MUST BE A SINGLE TEST) 5 am–noon Good Morning Noon-5 pm Good Afternoon 5 pm-8 pm Good Evening This greeting is to precede the earlier message