Presentation is loading. Please wait.

Presentation is loading. Please wait.

The relational operators

Similar presentations


Presentation on theme: "The relational operators"— Presentation transcript:

1 The relational operators
The syntax of the global isNaN method Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

2 © Akhilesh Bajaj, 2017. All rights reserved.
Chapter 3 © Akhilesh Bajaj, All rights reserved.

3 The logical operators in order of precedence
Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

4 The syntax of the if statement
Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

5 An if statement with else if and else clauses
Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

6 An if statement with a compound conditional expression
Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

7 The syntax of a while loop
Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

8 The syntax of a do-while loop
Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

9 © 2015, Mike Murach & Associates, Inc.
The syntax of a for loop Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

10 Functions: Compare Error Checking with loops and with GUI
Event Driven Programming Look at: SalesForceCompensationEventError web application Functions: Used to write code snippets that can be called on demand. In JavaScript functions can be written as variables, but in other languages, they are different. -Can be used to split the requirements so that each functions does a specific job. -Have zero or more parameters, and are called using corresponding arguments. -Usually return either a value, or an object (that can have many values). -To return back to caller, use the return statement. Fun In Class Assignment: Refactor the Calculator assignment using functions. Sketch the function signatures first.

11 © 2015, Mike Murach & Associates, Inc.
String Methods -ASCII versus Unicode -Understand the difference between \u0041 and 65 as code representations for the letter ‘A’. In Class Assignment: Write a program that prints out the following table: Character Code A Z a z Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

12 Fun In Class Assignment for Strings
Write a program that asks the user to key in strings, until the user types in *** to finish. Once the user has finished inputting the strings, the program prints out the string that had the maximum number of unique alphabets in it. It also prints out the count of these alphabets. To write this program, create a separate function called countAlpha(var s) that returns the number of unique alphabets in s. Create only one button on HTML page that is labeled: RunCountAlpha Sketch the overall code and the code for the function. Algorithm 1: Take all alphabets and check each one. Algorithm 2: Take first letter of string, scan forward. Eliminate duplicate characters. Then take second letter and repeat. Return length of final string. Use s=s. .replace(/\s/g, ''); to remove whitespace after every pass.

13 The syntax for creating an array
Arrays The syntax for creating an array The syntax for referring to an element of an array Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

14 How to add values to an array
The syntax for getting the length property of an array Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

15 Code that puts the number 1 through 10 into an array
Murach's JavaScript (2nd Ed.), C3 © 2015, Mike Murach & Associates, Inc.

16 Code that puts four totals in an array
Fun in Class Assignment: Write a Javascript method that gets an array as input, checks to make sure all elements are numbers, then returns the average of all numbers. Otherwise, returns false. Test the method with a main() function that generates an array of a random size (1-50) populated with random integers that range in value from 1-100) Murach's JavaScript (2nd Ed.), C3

17 Monte Carlo Simulation
Used to estimate the probability of events, say, in a game of chance. Basic Idea: Play the game many many times, and see how often your desired outcome occurs. Fun In Class Examples: -Write a program to calculate the probability of getting a heads on a coin toss. -Modify the program by adding another function to get the probability of at least 5 heads in a toss of 10 coins -Write another program to calculate the probability of getting at least a score of 6 on a toss of 2 die.

18 Some Array Methods in JavaScript
every(): Checks if every element in an array pass a test find(): Returns the value of the first element in an array that pass a test findIndex(): Returns the index of the first element in an array that passes a test indexOf(): Search the array for an element and returns its position isArray(): Checks whether an object is an array slice(): Selects a part of an array, and returns the new array sort(): Sorts the elements of an array toString(): returns components of an array as a string Loop through a sparse array: for(var i in arr1) { } Fun Example In Class: Write a function that returns the second highest value in an array of numbers. Check to make sure an array was passed and it has only numbers in it. © 2017, Akhilesh Bajaj All rights reserved.

19 © 2017, Akhilesh Bajaj. All rights reserved.
Associative Arrays -In an associative array, the index of each element is a unique string instead of an integer. -Integers are sequential but strings are not. So why would we have different elements, each accessed by a unique string? Are there situations where array elements need to be associated with a string, each? Can you think of any? Fun in class example: Write a web application to manage an array of customers. The array should be created with a size parameter. We want to capture the customer_id, customer_name, customer_address and customer_ for each customer. We should be able to add, update and delete customers. Assume customer_id and is unique for each customer. When inserting a customer, do a check to make sure no other customer has that id or . © 2017, Akhilesh Bajaj. All rights reserved.


Download ppt "The relational operators"

Similar presentations


Ads by Google