8
8
< title > Implementatino of indexOf method</ title >
9
9
10
10
< link rel ="stylesheet " href ="./styles/reset.css ">
11
- < link rel ="stylesheet " href ="./styles/variables.css ">
12
- < link rel ="stylesheet " href ="./styles/HeaderPage.css ">
13
- < link rel ="stylesheet " href ="./styles/Section.css ">
14
- < link rel ="stylesheet " href ="./styles/Code.css ">
15
- < link rel ="stylesheet " href ="./styles/utilities.css ">
11
+ < link rel ="stylesheet " href ="styles/utils/variables.css ">
12
+ < link rel ="stylesheet " href ="styles/main.css ">
13
+ < link rel ="stylesheet " href ="styles/components/HeaderPage.css ">
14
+ < link rel ="stylesheet " href ="styles/components/Section.css ">
15
+ < link rel ="stylesheet " href ="styles/components/Code.css ">
16
+ < link rel ="stylesheet " href ="styles/utils/utilities.css ">
16
17
17
18
< link href ="https://fonts.googleapis.com/css?family=Raleway:300,400,700 " rel ="stylesheet ">
18
19
< link href ="https://fonts.googleapis.com/css?family=Amatic+SC " rel ="stylesheet ">
19
20
< link rel ="stylesheet " href ="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css ">
20
21
</ head >
21
- < body >
22
22
23
+ < body >
23
24
< header class ="HeaderPage ">
24
25
< h1 class ="HeaderPage-mainTitle ">
25
26
Implementation
@@ -45,7 +46,7 @@ <h1 class="HeaderPage-mainTitle">
45
46
</ nav >
46
47
</ header >
47
48
48
- < main class ="content ">
49
+ < main class ="main- content ">
49
50
< div class ="Section ">
50
51
< h2 class ="Section-title "> Goal</ h2 >
51
52
< p class ="Section-paragraph "> Implement the method of the String.prototype indexOf</ p >
@@ -63,28 +64,65 @@ <h2 class="Section-title">Definition</h2>
63
64
< div class ="Section ">
64
65
< h2 class ="Section-title "> Proposal</ h2 >
65
66
< p class ="Section-paragraph ">
66
- The first thing I thought about was to create the needed < span class ="u-highlight "> guard clauses</ span > so I would let the main algorithm logic
67
- < span class =" u-highlight " > clean from all the concerns </ span > that would only make it dirtier
67
+ The first thing came to my mind were the < span class ="u-highlight "> guard clauses</ span > that helped me to < span class =" u-highlight " > isolate </ span > the code not related to the algorithm itself.
68
+ Doing this, instead of nested conditionals we got a "flat" list of conditionals, one after the other. It makes the code easier to read too.
68
69
</ p >
69
- < figure class ="Code u-vertical-space-top ">
70
- < pre >
71
- < code >
72
- < span class ="Code-comments "> // Truncate `from` because we can't secure that it's an integer</ span >
73
- < span class ="Code-keyword "> var</ span > _fromTruncated = < span class ="Code-keyword "> Math</ span > < span class ="Code-punctuation "> .</ span > trunc< span class ="Code-punctuation "> (</ span > from< span class ="Code-punctuation "> );</ span >
74
- < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > searchValue < span class ="Code-punctuation "> ===</ span > < span class ="Code-string "> ''</ span > < span class ="Code-punctuation "> &&</ span > _fromTruncated < span class ="Code-punctuation "> < </ span > 0< span class ="Code-punctuation "> ) {</ span >
75
- < span class ="Code-keyword "> return</ span > 0;
76
- < span class ="Code-punctuation "> }</ span >
77
70
78
- < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > searchValue < span class ="Code-punctuation "> ===</ span > < span class ="Code-string "> ''</ span > < span class ="Code-punctuation "> &&</ span > _fromTruncated < span class ="Code-punctuation "> > =</ span > < span class ="Code-keyword "> this</ span > < span class ="Code-punctuation "> .</ span > length< span class ="Code-punctuation "> ) {</ span >
79
- < span class ="Code-keyword "> return</ span > < span class ="Code-keyword "> this</ span > < span class ="Code-punctuation "> .</ span > length;
80
- < span class ="Code-punctuation "> }</ span >
71
+ < p class ="Section-paragraph u-vertical-space-top ">
72
+ The idea behind my solution is to iterate until we find a match with the first letter of the string we are looking for.< br >
73
+ If we don't find any, we return -1. If we do, we start to check the other character of the given string.
74
+ </ p >
75
+
76
+ < p class ="Section-paragraph u-vertical-space-top ">
77
+ Once we have found the first character, if the given string has < span class ="u-highlight "> length === 1</ span > it means we can return that index. If there are more characters to check, we go for it.< br >
78
+ </ p >
79
+
80
+ < p class ="Section-paragraph u-vertical-space-top ">
81
+ If the next character to check is different than the character in < span class ="u-highlight "> this</ span > we leave from that second loop and we go for another match with the first character of the given word.
82
+ </ p >
81
83
82
- < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > searchValue < span class ="Code-punctuation "> ===</ span > < span class ="Code-string "> ''</ span > < span class ="Code-punctuation "> ) {</ span >
83
- < span class ="Code-keyword "> return</ span > _fromTruncated< span class ="Code-punctuation "> ;</ span >
84
+ < pre class ="Code u-vertical-space-top ">
85
+ < code >
86
+ < span class ="Code-comments "> // Truncate `from` because we can't secure that it's an integer</ span >
87
+ < span class ="Code-keyword "> var</ span > _fromTruncated = < span class ="Code-keyword "> Math</ span > < span class ="Code-punctuation "> .</ span > trunc< span class ="Code-punctuation "> (</ span > from< span class ="Code-punctuation "> );</ span >
88
+ < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > searchValue < span class ="Code-punctuation "> ===</ span > < span class ="Code-string "> ''</ span > < span class ="Code-punctuation "> &&</ span > _fromTruncated < span class ="Code-punctuation "> < </ span > 0< span class ="Code-punctuation "> ) {</ span >
89
+ < span class ="Code-keyword "> return</ span > 0< span class ="Code-punctuation "> ;</ span >
90
+ < span class ="Code-punctuation "> }</ span >
91
+
92
+ < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > searchValue < span class ="Code-punctuation "> ===</ span > < span class ="Code-string "> ''</ span > < span class ="Code-punctuation "> &&</ span > _fromTruncated < span class ="Code-punctuation "> > =</ span > < span class ="Code-keyword "> this</ span > < span class ="Code-punctuation "> .</ span > length< span class ="Code-punctuation "> ) {</ span >
93
+ < span class ="Code-keyword "> return</ span > < span class ="Code-keyword "> this</ span > < span class ="Code-punctuation "> .</ span > length< span class ="Code-punctuation "> ;</ span >
94
+ < span class ="Code-punctuation "> }</ span >
95
+
96
+ < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > searchValue < span class ="Code-punctuation "> ===</ span > < span class ="Code-string "> ''</ span > < span class ="Code-punctuation "> ) {</ span >
97
+ < span class ="Code-keyword "> return</ span > _fromTruncated< span class ="Code-punctuation "> ;</ span >
98
+ < span class ="Code-punctuation "> }</ span >
99
+
100
+ < span class ="Code-keyword "> for</ span > < span class ="Code-punctuation "> (</ span > < span class ="Code-keyword "> var</ span > i < span class ="Code-punctuation "> =</ span > _fromTruncated< span class ="Code-punctuation "> ;</ span > i < span class ="Code-punctuation "> < </ span > this< span class ="Code-punctuation "> .</ span > length< span class ="Code-punctuation "> ;</ span > i++< span class ="Code-punctuation "> )</ span > {
101
+ < span class ="Code-keyword "> var</ span > thisCharsLeft < span class ="Code-punctuation "> =</ span > this.length - i< span class ="Code-punctuation "> ;</ span >
102
+ < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > this< span class ="Code-punctuation "> [</ span > i< span class ="Code-punctuation "> ]</ span > < span class ="Code-punctuation "> ===</ span > searchValue< span class ="Code-punctuation "> [</ span > 0< span class ="Code-punctuation "> ]</ span > < span class ="Code-punctuation "> )</ span > {
103
+ < span class ="Code-keyword "> var</ span > initial = i< span class ="Code-punctuation "> ;</ span >
104
+ < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > searchValue.length < span class ="Code-punctuation "> ===</ span > 1< span class ="Code-punctuation "> )</ span > < span class ="Code-punctuation "> {</ span >
105
+ < span class ="Code-keyword "> return</ span > initial< span class ="Code-punctuation "> ;</ span >
106
+
107
+ < span class ="Code-punctuation "> }</ span > < span class ="Code-keyword "> else if</ span > < span class ="Code-punctuation "> (</ span > thisCharsLeft > searchValue.length - 1< span class ="Code-punctuation "> )</ span > < span class ="Code-punctuation "> {</ span >
108
+ < span class ="Code-keyword "> for</ span > < span class ="Code-punctuation "> (</ span > < span class ="Code-keyword "> var</ span > j = 1< span class ="Code-punctuation "> ;</ span > j < searchValue .length < span class ="Code-punctuation "> ;</ span > j++< span class ="Code-punctuation "> )</ span > < span class ="Code-punctuation "> {</ span >
109
+ < span class ="Code-keyword "> if</ span > < span class ="Code-punctuation "> (</ span > this[++i] !== searchValue[j]< span class ="Code-punctuation "> )</ span > < span class ="Code-punctuation "> {</ span >
110
+ < span class ="Code-keyword "> break</ span > < span class ="Code-punctuation "> ;</ span >
111
+ < span class ="Code-punctuation "> }</ span > < span class ="Code-keyword "> else if</ span > < span class ="Code-punctuation "> (</ span > j < span class ="Code-punctuation "> ===</ span > < span class ="Code-punctuation "> (</ span > searchValue.length - 1< span class ="Code-punctuation "> )</ span > ) < span class ="Code-punctuation "> {</ span >
112
+ < span class ="Code-keyword "> return</ span > initial< span class ="Code-punctuation "> ;</ span >
113
+ < span class ="Code-punctuation "> }</ span >
114
+ < span class ="Code-punctuation "> }</ span >
115
+ < span class ="Code-punctuation "> }</ span >
116
+ < span class ="Code-punctuation "> }</ span >
84
117
< span class ="Code-punctuation "> }</ span >
85
- </ code >
86
- </ pre >
87
- </ figure >
118
+
119
+ < span class ="Code-keyword "> return</ span > -1< span class ="Code-punctuation "> ;</ span >
120
+ < span class ="Code-punctuation "> }</ span > < span class ="Code-punctuation "> ;</ span >
121
+
122
+
123
+
124
+ </ code >
125
+ </ pre >
88
126
</ div >
89
127
</ main >
90
128
0 commit comments