Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 11."— Presentation transcript:

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

2 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 Also @ http://perleybrook.umfk.maine.edu/SourceCode/http://perleybrook.umfk.maine.edu/SourceCode/ Html and XHTML examples http://perleybrook.umfk.maine.edu/samples/ 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

3 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

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

5 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");

6 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");

7 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] + " "); }

8 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');

9 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] + " ");

10 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

11 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

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

13 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; }


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

Similar presentations


Ads by Google