Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a JavaScript function fixCasing(str) that accepts a text as a parameter. The function must change
- the text in all regions as follows: <upcase>text</upcase> to uppercase; <lowcase>text</lowcase> to lowercase;
- <mixcase>text</mixcase> to mixed casing (randomly).
- Write JS program caseFixer.js that invokes your function with the sample input data below and prints
- the output at the console. */
- "use strict";
- function fixCasing(str) {
- var upperCaseRegex = /<upcase>(\w+\W*\w+)<\/upcase>/g;
- var match = upperCaseRegex.exec(str);
- while (match != null) {
- str = str.replace(match[0], match[1].toUpperCase());
- match = upperCaseRegex.exec(str);
- }
- var lowerCaseRegex = /<lowcase>(\w+\W*\w+)<\/lowcase>/g;
- match = lowerCaseRegex.exec(str);
- while (match != null) {
- str = str.replace(match[0], match[1].toLowerCase());
- match = lowerCaseRegex.exec(str);
- }
- function toMixCase(word) {
- var charsArr = word.split('').filter(Boolean);
- for (var i = 0; i < charsArr.length; i += 1) {
- var letter = charsArr[i];
- if (Math.round(Math.random())) {
- charsArr[i] = letter.toUpperCase();
- } else {
- charsArr[i] = letter.toLowerCase();
- }
- }
- return charsArr.join('');
- }
- var mixCaseRegex = /<mixcase>(\w+\W*\w+)<\/mixcase>/g;
- match = mixCaseRegex.exec(str);
- while (match != null) {
- str = str.replace(match[0], toMixCase(match[1]));
- match = mixCaseRegex.exec(str);
- }
- return str;
- }
- console.log(fixCasing(
- "We are <mixcase>living</mixcase> in a <upcase>yellow submarine</upcase>." +
- "We <mixcase>don't</mixcase> have <lowcase>anything</lowcase> else."));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement