Your SlideShare is downloading. ×
0
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
05 strings, numbers, and dates
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

05 strings, numbers, and dates

456

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
456
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. STRINGS, NUMBERS,AND DATESBuilt-in functions to make your life easier
  • 2. JavaScript is extremely capable•  In this chapter well delve into •  Strings •  Numbers •  Dates •  Efficiency
  • 3. WORKING WITH STRINGS
  • 4. Strings• String are text data. Lets look at: •  Length •  Changing case •  Simple search •  Getting a substring •  Using regular expressions
  • 5. You get a strings length from string.lengthvar password =document.getElementById(password).value;!if (password.length < 8 ) {! alert(That password is too short.);!}!
  • 6. Changing case of a stringvar x = theString.toUpperCase()!var y = theString.toLowerCase()!•  These return the changed string but do not change the string itself
  • 7. You can do a simple search within stringsvar loc = theString.indexOf(str);!var loc = theString.lastIndexOf(str);!
  • 8. You can get substrings from strings•  var sub = theString.slice(start,end);•  start is zero-based•  end is one-based •  if less than zero, they will reach backwards into the string
  • 9. REGULAR EXPRESSIONS
  • 10. JavaScripthonorsregularexpressions var regex = /regularExpression/g;
  • 11. You can find the location of a regex withina string•  var whereInString = theString.search(regex);•  The result will be its zero-based location within the string•  -1 => not found•  Just like indexOf()
  • 12. You can grab the portion that matched with match()var result = ! string.match(regex);!// result is an array!!
  • 13. match() gives you an array back with allthe matches•  var matches = theString.match(regex);•  var numMatches = matches.length;•  var firstMatch = numMatches[0];•  var nextMatch = numMatches[1];•  … and so on.
  • 14. And you can replace the string withreplace()•  var newString = orig.replace(regex, new);•  Does not change the original string•  Replaces every occurrence of regex with new•  Example:var date = 10.28.2012;!var regex = /./g;!date = date.replace(regex, /);!•  date is now 10/28/2012
  • 15. NUMBER TOOLS
  • 16. Working with numbersJavaScript has the ability to work with numbers. Lets lookat some of the abilities in this next section.
  • 17. You can change strings to numbers•  Several ways:var num = Number(theString);! •  Returns NaN if theString isnt numericvar num = parseInt(theString);! •  Reads theString char by char until it gets to a non [0-9] character and stops. This includes .var num = parseFloat(theString);! •  Reads until it gets to a non [0-9] or . and stops
  • 18. if (isNaN(theString)) {! … ! }!You can test ifa variable is anumber or not
  • 19. You can round numbers with Math.round()•  Original number is not changed•  Returns a rounded numbervar rounded = Math.round(number);! •  Rounds to nearest whole numbervar roundedUp = Math.ceil(number);! •  Always rounds upvar roundedDown = Math.floor(number);! •  Always rounds down
  • 20. You can round a number to any numberof significant digits•  var rounded = oldNumber.toFixed(n);•  Where n is the number of digits to the right of the decimal•  Example: To make something look like currency … var number = 101.37501;! var currency = $ +! number.toFixed(2);! // currency now is $101.38!
  • 21. You can make random numbers with Math.random() •  Returns a number between 0 and 1•  Get any random number between x and y with:Math.floor((Math.random() * (y - x + 1)) + x);!
  • 22. DATES AND TIMES
  • 23. You can also work with dates and times//Todays date!var now = new Date();!//Christmas, 2012!var then = new Date(2012, 11, 25);!//3:23 pm same day!then = new Date(2012, 11, 25, 15, 23);!
  • 24. Date functions show different parts of thedate•  getFullYear() // 4-digit year•  getMonth() // integer between 0 (Jan.) & 11 (Dec.)•  getDate() // integer between 1 & 31•  getDay() // integer between 0 (Sunday) & 6 (Saturday)•  getHours()•  getMinutes()•  getSeconds()•  getTime() // number of milliseconds since Jan 1, 1970
  • 25. To get timespans …•  var now = new Date();•  var then = new Date(2014, 11, 25);•  var diff = then – now;•  var daysTill = diff / (1000 * 60 * 60 * 24);
  • 26. Hands on dates and times
  • 27. OTHER TECHNIQUES
  • 28. There are someother coolJavaScript tricksyou can use•  Lets look at some …
  • 29. Reuse JavaScript files as much aspossible•  Theyll be cached, saving download time.•  Big files = slow download•  Dont download things that arent needed•  So, best practice is•  One JS file per page with unique functions•  One global with shared functions
  • 30. Use the ternary operator(condition) ? A : B ;!var status = (a == correct) ? good : bad;!
  • 31. Minifyingmakes yourJavaScriptload quickly•  YUI Compressor•  http://developer.yahoo.com/yui/compressor•  http://refresh-sf.com/yui
  • 32. Conclusion•  Strings•  Regex•  Converting with Number(), parseInt(), parseFloat()•  isNan()•  Rounding with round(), ceil(), floor(), and toFixed()•  Random numbers with Math.random()•  Dates•  Ternary statement•  Switch statement•  Minifying code

×