Exercise - JS Core Objects

by suresh veguru

JavaScript

/**
 * String manipulation
 *
 * Create a function that takes a string and performs some operations on it.
 * The function should:
 * 1) Replace the word "today" with the current Date (format: YYYY-MM-DD)
 * 2) Replace the word "pi" with the numeric value of PI, to the 2nd decimal place
 * 3) Count how many occurrences of the letter X there are and output the count to the console
 * It then returns the modified string
 *
 */

function manipulate(str)
{
    var date = new Date();
    var datestr = date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate();
   
    str = str.replace(/\btoday\b/, new Date().toGMTString());
    
    str = str.replace(/\bpi\b/,Math.PI);
    
    return str;
}

alert(manipulate("date is today and value pie is pi"));