File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /***************************************************************************************
2
+ * *
3
+ * CODERBYTE BEGINNER CHALLENGE *
4
+ * *
5
+ * Ex Oh *
6
+ * Using the JavaScript language, have the function ExOh(str) take the str parameter *
7
+ * being passed and return the string true if there is an equal number of x's and o's, *
8
+ * otherwise return the string false. Only these two letters will be entered in the *
9
+ * string, no punctuation or numbers. For example: if str is "xooxxxxooxo" then the *
10
+ * output should return false because there are 6 x's and 5 o's. *
11
+ * *
12
+ * SOLUTION *
13
+ * My solution accounts for the one outlier which is when the length of the string *
14
+ * is not even. Since the count of Xs and Os have to be equal then the only way for *
15
+ * this to happen is if the length of the string is an even number. I am going to *
16
+ * loop through the string and count every X. Then I am going to compare this count *
17
+ * to half the length of the string. If they are equal then you have equal number of *
18
+ * Xs and Os. *
19
+ * *
20
+ * Steps for solution *
21
+ * 1) Handle outlier first by seeing if str length is odd *
22
+ * 2) Loop thru each letter and count the number of Xs *
23
+ * 3) Compare count to 1/2 length of str and if same then return true *
24
+ * 4) Else return false *
25
+ * *
26
+ ***************************************************************************************/
27
+ function ExOh(str) {
28
+
29
+ if (str.length % 2 === 1) {
30
+ return false;
31
+ }
32
+ else {
33
+ var tot = 0;
34
+ for (var i = 0; i < str.length; i++) {
35
+ if (str[i] === "x") {
36
+ tot++;
37
+ }
38
+ }
39
+
40
+ if (tot === (str.length / 2)) {
41
+ return true;
42
+ }
43
+ else {
44
+ return false;
45
+ }
46
+
47
+ }
48
+
49
+ }
You can’t perform that action at this time.
0 commit comments