-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcb_SimpleSymbols.js
160 lines (101 loc) · 3.24 KB
/
cb_SimpleSymbols.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Using the JavaScript language, have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.
function SimpleSymbols(str) {
//find positions of letters in string
var arr = str.split("");
var position = [];
var result = true;
var alphabet = /[a-zA-Z]/;
for (var i in arr){
if(alphabet.test(arr[i])){
position.push(i);
}
}
//check if + on both sides of positions
for (var i in position){
if (position.length = 0){
return false
}
if (arr[parseInt(position[i])+1] !== "+" || arr[parseInt(position[i])-1] !== "+"){
return false
}
}
return result;
}
// My test
var str = "+d+=3=+s+";
console.log(SimpleSymbols(str)); //solution: "true"
/* Non RegEx Solution
function SimpleSymbols(str) {
// code goes here
//create a filter for alphabet
var isLetter = function(testChar){
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var arr = alpha.split("");
for (var i in arr){
if (testChar === arr[i]){
return true;
}
}
return false;
};
//find positions of letters in spring
var arr = str.split("");
var position = [];
var result = true;
for (var i in arr){
if(isLetter(arr[i])){
position.push(i);
}
}
console.log(position);
if (position[0] = undefined){
return false
}//check if + on both sides of positions
for (var i in position){
var afterPosition = parseInt(position[i])+1;
var beforePosition = parseInt(position[i])-1;
if (arr[afterPosition] !== "+" || arr[beforePosition] !== "+"){
return false
}
}
return result;
}
var str = "++c+++a++"
console.log(SimpleSymbols(str));
/* CoderByte Ready
function SimpleSymbols(str) {
// code goes here
//create a filter for alphabet
var isLetter = function(testChar){
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var arr = alpha.split("");
for (var i in arr){
if (testChar === arr[i]){
return true;
}
}
return false;
};
//find positions of letters in spring
var arr = str.split("");
var position = [];
var result = true;
for (var i in arr){
if(isLetter(arr[i])){
position.push(i);
}
}
//check if + on both sides of positions
for (var i = 1; i <= arr.length - 2; i++){
var before = i-1;
var after = i+1;
if ( arr[position[before]] !== "+" || arr[position[after]] !== "+"){
return false;
}
}
return result;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
print(SimpleSymbols(readline()));
*/