Section A.3. Chapter 4


A.3. Chapter 4

  1. Use the String.split method, passing in a comma (,) as delimiter.

  2. Word boundaries are particularly useful if you want a separate word, but being given a string that could match within another word:

  3. var regexp = /\bfun\b/; var str = "The fun of functions is that they are functional."; var result = str.replace(regexp,"power");

  4. There is no Date function that manipulates weeks, but we know that a week is 7 days at 24 hours a day, for a total of 168 hours. Use the getHours method to get the current dateâ??s hours, add this value, reset the hours, and then print out the date. Other approaches can also be used and are left for your own exploration:

  5. var dtNow = new Date(â??â??); var hours = dtNow.getHours(â??â??); hours+=168; dtNow.setHours(hours); document.writeln(dtNow.toString(â??â??));

  6. Math.floor can be used to round the number down; Math.ceil can round the number up.

  7. The answer is:

  8. var str = "apple.orange-strawberry,lemon-.lime"; var regexp = /[\.|-]/g; var result = str.replace(regexp,','); var arrayValues = result.split(','); for (var i = 0; i < arrayValues.length; i++) {    document.writeln(arrayValues[i] + "<br />"); }




Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net