-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS_Str_Manip2.js
104 lines (95 loc) · 3.31 KB
/
JS_Str_Manip2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var message = "THE QUICK WN F JUMS VE THE LAZY G THA QOECK BRIWN FIX JOMPS FgitVAR THA LUZY DIG THA QECK
IWN FI JMS IVA THA LUZY IG QUICK JUMS LAZY QUICK BROWN JUMPS OVER LAZY THU QIACK BREWN FEX JIMPS EVUR THU
LOZY DEG EHT KCIUQ BROWN FOX JUMPS REVO EHT LAZY DOG THA QOECK WN F JOMS ONDIPINDINT VA THA LUZY G AHT
KCEOQ BRIWN FIX JOMPS LVAR AHT LUZY DIG";
//Remove all letters that appear in the word 'PUZZLE'
function func1(message) {
return message.replace(/[PUZZLE]/g,"");
};
//Replace each vowel with the one before it (a→u, e→a, i→e, o→i, u→o).
function func2(message) {
var aMessage = message.split("");
for ( var i = 0; i < aMessage.length; ++i ) {
switch( aMessage[i] ) {
case 'a': case 'A':
aMessage.splice(i, 1, 'U');
break;
case 'e': case 'E':
aMessage.splice(i, 1, 'A');
break;
case 'i': case 'I':
aMessage.splice(i, 1, 'E');
break;
case 'o': case 'O':
aMessage.splice(i, 1, 'I');
break;
case 'u': case 'U':
aMessage.splice(i, 1, 'O');
break;
}
}
return aMessage.join("");
}
//Reverse every word that starts with a letter in the second half of the alphabet.
function func5(message) {
var aMessage = message.split(" ");
var nz = "NOPQRSTUVWXYZ".split("");
for ( var i = 0; i < aMessage.length; ++i ) {
var letterToMatch = aMessage[i].charAt(0);
for ( var j = 0; j < nz.length; ++j ) {
if ( letterToMatch === nz[j] )
aMessage.splice( i, 1, aMessage[i].split("").reverse().join("") );
}
}
return aMessage.join(" ");
}
//Insert "P" one left of the middle of every word that has an even number of letters.
function func7(message) {
var aMessage = message.split(" ");
for ( var i = 0; i < aMessage.length; ++i ) {
if ( aMessage[i].length === 2 ) {
var aWord = aMessage[i].split("");
aWord.splice( 1, 0, "P" );
aMessage[i] = aWord.join("");
}
else if ( aMessage[i].length % 2 === 0 ) {
var insertPos = aMessage[i].length / 2 - 1;
aWord = aMessage[i].split("");
aWord.splice( insertPos, 0, "P" );
aMessage[i] = aWord.join("");
}
}
return aMessage.join(" ");
}
//Remove every word that doesn't start with a vowel.
function func8(message) {
var aMessage = message.split(" ");
var vowels = /[AEIOU]/;
for ( var i = 0; i < aMessage.length; ++i ) {
var letterToCheck = aMessage[i].charAt(0);
if ( letterToCheck.match(vowels) ) {
continue;
} else {
aMessage[i] = "";
}
}
aMessage = aMessage.join(" ");
return aMessage.replace(/\s/g, ' ');
}
//Remove all words that have 3 letters or less.
function func9(message) {
var aMessage = message.split(" ");
for ( var i = 0; i < aMessage.length; ++i ) {
if ( aMessage[i].length > 3 )
continue;
else
aMessage[i] = "";
}
aMessage = aMessage.join(" ");
return aMessage.replace(/\s/g, ' ');
}
//Remove all whitespaces.
function func10(message) {
var aMessage = message.split(" ");
return aMessage.join("");
}