Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 11.

Slides:



Advertisements
Similar presentations
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
Advertisements

1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
Building Java Programs
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
© 2010 Pearson Education, Inc. All rights reserved.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 12.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
COS 381 Day 22. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
Cos 381 Day 9. © 2006 Pearson Addison-Wesley. All rights reserved. Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Teaching Programming Concepts with JavaScript: No Software Package Required Paul Addison, Ivy Tech Community College Lafayette, Indiana.
JavaScript Lecture 6 Rachel A Ober
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 JavaScript and Dynamic Web pages. Objectives Static Web pages Dynamic Web pages JavaScript Variables Assignments. JavaScript Functions –(prompt(“”,””)
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exam scores by range 3-1.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11.5 Lines and Curves in Space.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1 Functions.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 5 JavaScript.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
COP 3813 Intro to Internet Computing Prof. Roy Levow Lecture 4 JavaScript.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
CSC 121 Computers and Scientific Thinking Fall Interactive Web Pages.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 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 JavaScript and Dynamic Web Pages Lecture 7. 2 Static vs. Dynamic Pages  A Web page uses HTML tags to identify page content and formatting information.
Javascript Arrays Ch.19. Array definition & for loop var quiz = [85,90,100,0]; // creates an array var ex = []; ex[0] = 89; // add the quiz grades quizTotal.
Recap: If, elif, else If <True condition>:
Chapter 10 - JavaScript: Functions
Random numbers Taken from notes by Dr. Neil Moore
The relational operators
© Akhilesh Bajaj, All rights reserved.
Dry run Fix Random Numbers
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 11

5-2 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard Html and XHTML examples Assignment 2 Corrected 1 A, 2 B’s, 2 C’s, 1 D and 1 F Major! programming issues Lack of algorithm generation Assignment 3 will be creating a board game using JavaScript and DOM Will be posted next week Capstone Progress reports Due in One Week Review of assignment 2 JavaScript random Numbers

5-3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Assignment 3 UUUK MFKK FKFM UMFK Randomly placed letters Users may swap adjacent tiles Cost for swap? Spelling UMFK scores points Correctly placed tiles are removed Down and across Empty slots are filled with random Letters Gravity? Games continues till ? X numbers of tiles game missing letter (no M’s) max score score goes negative User quits Extra Features Animations larger than 4 X 4 Spelling on diagonals

5-4 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Ex st 20 Fibonacci numbers var first = 1, second = 1, next, count; document.write("First 20 Fibonacci Numbers "); document.write(" "); for (count = 3; count <= 20; count++) { next = first + second; document.write(count + " - " + next + " "); first = second; second = next; }

5-5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley EX 4.5 Legal, illegal name var inStr, inValid, splitStr; inStr = prompt("Enter your full name, Last,First,MiddleInitial, separated by commas"); inValid = inStr.search(/^[A-Z][a-z]+,\s?[A-Z][a-z]+,\s?[A-Z]\.?$/); (on page 213) //document.write(" ", inValid); if (inValid == 0 ) { splitStr = inStr.split(","); if (splitStr[0].lenght > 15 || splitStr[1].lenght > 16) document.write("illegal") else document.write("legal"); } else document.write("illegal");

5-6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Ex 4.5 better! var inStr, inValid; inStr = prompt("Enter your full name, Last,First,MiddleInitial, separated by commas", "There, Me, I."); inValid = inStr.search(/^[A-Z][a-z]{1,14},\s?[A-Z][a-z]{1,14},\s?[A-Z]\.?$/); //document.write(" ", inValid); if (inValid == 0 ) document.write(inStr + " is a legal name"); else document.write(inStr + " is an illegal name");

5-7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley EX 4.7 Ascending or Descending Sort var order, str, words, word_len, count; // Get the input str = prompt("Please input your sentence", ""); order = prompt("What order? (ascending or descending)", ""); // If the order is recognized, issue an error message if (order != "descending" && order != "ascending") document.write("Error - order is incorrectly specified "); // Otherwise, do the sort, depending on the requested order else { var words = str.split(" "); words =words.sort(); if (order == “descending") words = words.reverse(); // Write out the results words_len = words.length; for (count = 0; count < words_len; count++) document.write(words[count] + " "); }

5-8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley EX 4.10 First vowel function var inStr, location; function first_vowel (word) { var position; position = word.search(/[aeiou]/i); return position; } inStr = prompt("Enter some text"); location = first_vowel(inStr) ; if (location >=0) document.write('The first vowel is "' + inStr.charAt(location) + '" in position ' + location ); else document.write('There are no vowels in the string');

5-9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Average of arrays function matrix_avg(matrix) { var avg_matrix = new Array(matrix.length); var sum; for (var i = 0; i < matrix.length; i++) { sum = 0; for (var j = 0; j < matrix[i].length; j++) sum = sum + matrix[i][j]; avg_matrix[i] = sum / matrix[i].length; } return avg_matrix; } var inMatrix = [[1,3,5,6,8,10],[2,5,7,12,67,1],[23,45,67]]; outMatrix = matrix_avg(inMatrix); for (var i=0; i < outMatrix.length; i++) document.write("Row " + i + " average -> " + outMatrix[i] + " ");

5-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley JavaScript random numbers At the heart of almost all games is a random number generator Introduces unpredictability through probability Dice roll Ensures “fair” play Prevents repeatability (same games every time) Most random number generators are pseudo-random generators Based on “seeds” Same seed  same ordering of resulting numbers Most common seed is the computer’s system clock You need random seeds to make random numbers (there's the catch-22!) Good enough for most things JavaScript has a method that produces pseudo-random numbers in the Math object Math.random();  random number between 0 and 1

5-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Math.random() Random Number between 0 and 1 are not very useful Need a greater “range” Math.floor(Math.random()*11)  0 to 10 (11 possibilities) Math.floor(Math.random()*10)  0 to 9 (10 possibilities) Always starts at 0 (also not as useful) Consider a die 6 sides  1 to 6 dots (Math.floor(Math.random()*6) + 1)  1 to 6 general format Math.floor(Math.random() * ”range” ) + “shift”  integer numbers starting with “shift” and going to “range” Lets create a dice rolling program

5-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley In Class Work Dice roller Dice 1 Dice 2 Total

5-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dice roller Math.floor(Math.random()*6) + 1)  1 to 6 function roll() {return Math.floor(Math.random()*6) + 1; } function rollDice() { var d1=document.getElementById('d1'); var d2=document.getElementById('d2'); var T=document.getElementById('T'); die1=roll(); d1.value=die1; die2=roll(); d2.value=die2; total = die1 +die2; T.value =total; }