/**
* 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
* 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 should then return the modified string.
*
*/
/*var str = "today is pi day";
str = str.replace("today", "now");
str = str.replace("pi", Math.round(Math.PI * 100) / 100);
console.log(str);
*/
function stringFun(str) {
var today = Date.now().toString(),
count = 0;
var result = str.replace(/\b\w+\b/g, function (word) {
count += (word.match(/x/gi) || []).length;
switch (word) {
case "today":
case "Today":
return today;
case "pi":
case "Pi":
return Math.round(Math.PI * 100) / 100;
default:
return word;
}
});
console.log("number of x chars is " + count);
return result;
}
var a = stringFun("today is xoxo pi day");
console.log(a);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.