590 likes | 725 Views
JavaScript Storyteller Project 2B. Putting a big project together. D.A. Clements. Objectives. Understand how a larger project is built from the basic programming concepts Understand how to declare multiple functions Understand how to call functions from Event handlers Other functions.
E N D
JavaScript Storyteller Project 2B Putting a big project together D.A. Clements D.A. Clements, MLIS, UW iSchool
Objectives • Understand how a larger project is built from the basic programming concepts • Understand how to declare multiple functions • Understand how to call functions from • Event handlers • Other functions D.A. Clements, MLIS, UW iSchool
Small programming modules that can be called as needed Functions D.A. Clements, MLIS, UW iSchool
Passing Values to Functions • Definition syntax function name(parameter) { //statement body x = x * parameter; } • Call syntax onclick="name(argument)" D.A. Clements, MLIS, UW iSchool
Caps Function Declaration function caps(word) { } D.A. Clements, MLIS, UW iSchool
Caps function—change to LC function caps(word) { var word = word.toLowerCase(); } User entered: A N N ? A n n ? a n n ? Change to: a n n D.A. Clements, MLIS, UW iSchool
Caps function—change to LC function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); } word: a n n D.A. Clements, MLIS, UW iSchool
Caps function—grab first letter function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); } word: 0 1 2 a n n String methods treat words like arrays! D.A. Clements, MLIS, UW iSchool
Caps function—save first letter function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); } word: 0 1 2 an n firstLetter: a D.A. Clements, MLIS, UW iSchool
Caps function—firstLetterUC function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); firstLetter = firstLetter.toUpperCase(); } word: 0 1 2 a n n firstLetter: a D.A. Clements, MLIS, UW iSchool
Caps function—firstLetter UC function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); firstLetter = firstLetter.toUpperCase(); } word: 0 1 2 a n n firstLetter: A D.A. Clements, MLIS, UW iSchool
Caps function—rest of word function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); firstLetter = firstLetter.toUpperCase(); var restOfWord = word.substr(1); } word: 0 1 2 a n n firstLetter: A lastIndex: 2 D.A. Clements, MLIS, UW iSchool
Caps function—restOfWord function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); firstLetter = firstLetter.toUpperCase(); var lastIndex = word.length -1; var restOfWord = word.substr(1,lastIndex); } word: 0 1 2 a n n firstLetter: A restOfWord: n n D.A. Clements, MLIS, UW iSchool
Caps function—return function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); firstLetter = firstLetter.toUpperCase(); var lastIndex = word.length -1; var restOfWord = word.substr(1, lastIndex); var cappedWord = firstLetter+restOfWord; } word: 0 1 2 a n n firstLetter: A restOfWord: n n D.A. Clements, MLIS, UW iSchool
Caps function—return function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); firstLetter = firstLetter.toUpperCase(); var lastIndex = word.length -1; var restOfWord = word.substr(1, lastIndex); var cappedWord = firstLetter + restOfWord; } word: 0 1 2 a n n firstLetter: A restOfWord: n n cappedWord A n n D.A. Clements, MLIS, UW iSchool
Caps function—return function caps(word) { var word = word.toLowerCase(); var firstLetter = word.charAt(0); firstLetter = firstLetter.toUpperCase(); var lastIndex = word.length -1; var restOfWord = word.substr(1, lastIndex); var cappedWord = firstLetter + restOfWord; returncappedWord; } word: 0 1 2 a n n firstLetter: A restOfWord: n n cappedWord A n n D.A. Clements, MLIS, UW iSchool
How do you call caps()? • That’s all well and good • But how and where do you call a function? D.A. Clements, MLIS, UW iSchool
Functions—the call • Call the function where you need to run it: • Event handlers • In the input field or button or image tag: onclick=“tellStory()”; • Inside functions or scripts • Like tellStory() D.A. Clements, UW Information School
Story story += ‘<p>Once upon a time, there lived a <span class="replace">' + person + '</span> named ' + firstName + '.</p>‘; D.A. Clements, UW Information School
Calling caps() in the story story += <p>Once upon a time, there lived a <span class="replace">' + person + '</span> named ' + caps(firstName)+ '.</p>'; D.A. Clements, UW Information School
Declaring the first function <head> <title>D.A.'s JavaScript Storyteller</title> <script type=“JavaScript”> function tellStory() { //statements } </script> </head> D.A. Clements, UW Information School
Declaring a second function <script type=“JavaScript”> function tellStory() { //statements } function caps(word) { //statements } </script> D.A. Clements, UW Information School
Declaring a third function <script type=“JavaScript”> function tellStory() { //statements } function capitalize(word) { //statements } function a_or_an(word) { //statements } </script> D.A. Clements, UW Information School
Declaring variables <script type=“JavaScript”> var variables; //declare variables //also bring user data over from form function tellStory() { //statements } function capitalize(word) { //statements } function a_or_an(word) { //statements } </script> D.A. Clements, UW Information School
a_or_an function function a_or_an(word) { } D.A. Clements, UW Information School
a_or_an function strategy function a_or_an(word) { // basic strategy: make character lower case, // then see if it's in a string that contains all // of the vowels } D.A. Clements, UW Information School
a_or_an function—vowels array function a_or_an(word) { /* basic strategy: make character lower case, then see if it's in a string that contains all of the vowels */ var vowels = "aeiou"; } vowels: 0 1 2 3 4 a e i o u D.A. Clements, UW Information School
a_or_an function—pass value function a_or_an(word) { var vowels = "aeiou"; var c = word.charAt(0); } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r D.A. Clements, UW Information School
a_or_an function—first letter function a_or_an(word) { var vowels = "aeiou"; var c = word.charAt(0); } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r D.A. Clements, UW Information School
a_or_an function—first letter function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: P D.A. Clements, UW Information School
a_or_an function—lower case function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); firstLetter = firstLetter.toLowerCase(); } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: p D.A. Clements, UW Information School
a_or_an function—vowel? function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); firstLetter = firstLetter.toLowerCase(); var test = vowels.indexOf(firstLetter); } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: p D.A. Clements, UW Information School
a_or_an function function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); firstLetter = firstLetter.toLowerCase(); var test = vowels.indexOf(firstLetter); if (test < 0) { //something goes here } else { //something goes here } } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: p D.A. Clements, UW Information School
a_or_an function function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); firstLetter = firstLetter.toLowerCase(); var test = vowels.indexOf(firstLetter); if (test < 0) { return "a " + word; } else { //something goes here } } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: p D.A. Clements, UW Information School
a_or_an function function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); firstLetter = firstLetter.toLowerCase(); var test = vowels.indexOf(firstLetter); if (test < 0) { return "a " + word; } else { return "an " + word; } } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: p D.A. Clements, UW Information School
a_or_an function function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); firstLetter = firstLetter.toLowerCase(); var test = vowels.indexOf(firstLetter); if (test < 0) { return "a " + word; } else { return "an " + word; } } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: p D.A. Clements, UW Information School
a_or_an function function a_or_an(word) { var vowels = "aeiou"; var firstLetter = word.charAt(0); if (vowels.indexOf(firstLetter.toLowerCase())< 0) { return "a " + word; } else { return "an " + word; } } vowels: 0 1 2 3 4 a e i o u word: 0 1 2 3 P E a r firstLetter: p D.A. Clements, UW Information School
setupStoryWindow() • Sometimes, this code won't work: var header = '<head><link href="story.css" rel="stylesheet" type="text/css"><title>' +name+ "\'s " + 'Story</title></head>'; • Try this code instead: var header = '<head>'; header += '<link href="story.css" rel="stylesheet" type="text/css"><title>' +name+ "\'s " + 'Story</title></head>'; D.A. Clements, UW Information School
Stitching together strings and variables Concatenating the story D.A. Clements, UW Information School
Adding to story • story = story + a_or_an(fruit); • story += a_or_an(fruit); D.A. Clements, UW Information School
White Space Beware! • One problem with white space • If it's in a string • It can stop your program dead D.A. Clements, UW Information School
A stop-it dead story killer…. HTML JavaScript Fumbles it • Handles white space var story = ""; story = '<p>Once upon a time, there lived a wizened old wizard who lived in the heart of an oak tree.</p> <p>He lived for vast eons in the tree, dreaming, until one day....' D.A. Clements, UW Information School
A stop-it dead story killer…. HTML JavaScript Fumbles it • Handles white space var story = ""; story = '<p>Once upon a time, there lived a wizened old wizard who lived in the heart of an oak tree.</p> <p>He lived for vast eons in the tree, dreaming, until one day....' GAP D.A. Clements, UW Information School
Solution… HTML JavaScript Fumbles it • Handles white space var story = ""; story = '<p>Once upon a time, there lived a wizened old wizard who lived in the heart of an oak tree.</p> <p>He lived for vast eons in the tree, dreaming, until one day....' Close it up! D.A. Clements, UW Information School
Another solution… HTML JavaScript Fumbles it • Handles white space var story = ""; story = '<p>Once upon a time, there lived a wizened old wizard who lived in the heart of an oak tree.</p>'; story += <p>He lived for vast eons in the tree, dreaming, until one day....' D.A. Clements, UW Information School
Relax! No worries! • When you print your new story to the screen with document.write() • The css won't show • Wait for css to show on the last page of the instructions when you build the setupStoryWindow() function. D.A. Clements, UW Information School
Setting those gender pronouns based on user input…. Arrays and conditionals D.A. Clements, UW Information School
Overview • Gender changes based on what the user chose in the dropdown menu • Arrays set up the series of pronouns for each gender • Use conditionals to choose which array to use D.A. Clements, UW Information School
The gender arrays var MalePronouns = new Array ("he","his","him","man","men"); var FemalePronouns = new Array ("she","her","her","woman","women"); var PersonPronouns = new Array ("one","one's","one","person","persons"); var PeoplePronouns = new Array ("they","their","them","people","people"); var gender; D.A. Clements, UW Information School
Gender in your story • Each gender has its own pronoun array • Edit the array to include the words needed by your story • Replace Man with King, Prince, Boy, or Uncle • Replace Woman with Queen, Princess, Girl, or Aunt, etc. D.A. Clements, UW Information School