© Akhilesh Bajaj, All rights reserved.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
Information Technology Center Hany Abdelwahab Computer Specialist.
Fundamentals of Python: From First Programs Through Data Structures
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Murach’s JavaScript, C2© 2009, Mike Murach & Associates, Inc.Slide 1.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Using Client-Side Scripts to Enhance Web Applications 1.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
If statements while loop for loop
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Fundamentals of Python: First Programs
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
JavaScript, Fourth Edition
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Chapter 2 Murach's JavaScript and jQuery, C2© 2012, Mike Murach & Associates, Inc.Slide 1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
JavaScript 101 Lesson 6: Introduction to Functions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
REEM ALMOTIRI Information Technology Department Majmaah University.
DEVRY CIS 170 C I L AB 5 OF 7 A RRAYS AND S TRINGS Check this A+ tutorial guideline at
CSS Colors, JavaScript Variables, Conditionals and Basic Methods
JavaScript, Sixth Edition
Chapter 11 - JavaScript: Arrays
© 2016, Mike Murach & Associates, Inc.
Introduction To Repetition The for loop
© 2015, Mike Murach & Associates, Inc.
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
© 2016, Mike Murach & Associates, Inc.
JavaScript: Functions
JavaScript Functions.
Chapter 8 Arrays Objectives
Chapter 7 Arrays.
JavaScript: Functions.
The relational operators
CSS Colors, JavaScript Variables, Conditionals and Basic Methods
Computer Science 2 Hashing
Chapter 8 Arrays Objectives
Chapter 10 Lists.
CIS16 Application Development and Programming using Visual Basic.net
© 2015, Mike Murach & Associates, Inc.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Writing Functions( ) (Part 4)
Fundamentals of Python: First Programs
Chapter 8 Arrays Objectives
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 9: More About Data, Arrays, and Files
Introduction to Computer Science
Web Programming and Design
Arrays.
Murach's JavaScript and jQuery (3rd Ed.)
Murach's JavaScript and jQuery (3rd Ed.)
Murach's JavaScript and jQuery (3rd Ed.)
Presentation transcript:

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

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

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

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

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

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

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.

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.

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.

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.

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

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

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.

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 split(): Split a string into an array of sub strings. Loop through a sparse array: for(var i in arr1) { } Fun Example In Class: Write a program that has 4 text areas and one generate button. The first 3 text areas contain a list of nouns, verbs and adjectives, respectively. When the generate button is pressed, a prompt asks for how many sentences you need, then randomly generates those sentences in sequence. The syntax for a sentence is: <a, an or the>[adjective]<noun><verb><<a, an or the>[adjective]<noun>. © 2017, Akhilesh Bajaj All rights reserved.

© 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_email for each customer. We should be able to add, update and delete customers. Assume customer_id and email is unique for each customer. When inserting a customer, do a check to make sure no other customer has that id or email. © 2017, Akhilesh Bajaj. All rights reserved.