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(  );
    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 = ",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 />");

Get Learning JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.