|
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"> |
11 | 12 | <link rel="stylesheet" href="./styles/HeaderPage.css">
|
12 | 13 | <link rel="stylesheet" href="./styles/Section.css">
|
| 14 | + <link rel="stylesheet" href="./styles/Code.css"> |
| 15 | + <link rel="stylesheet" href="./styles/utilities.css"> |
13 | 16 |
|
14 | 17 | <link href="https://fonts.googleapis.com/css?family=Raleway:300,400,700" rel="stylesheet">
|
15 | 18 | <link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet">
|
@@ -47,8 +50,43 @@ <h1 class="HeaderPage-mainTitle">
|
47 | 50 | <h2 class="Section-title">Goal</h2>
|
48 | 51 | <p class="Section-paragraph">Implement the method of the String.prototype indexOf</p>
|
49 | 52 | </div>
|
50 |
| - </main> |
51 | 53 |
|
| 54 | + <div class="Section"> |
| 55 | + <h2 class="Section-title">Definition</h2> |
| 56 | + <p class="Section-paragraph"> |
| 57 | + The <span class="u-highlight">indexOf()</span> method returns the index within the calling <span class="u-highlight">String</span> object of |
| 58 | + the first occurrence of the specified value, starting the search at <span class="u-highlight">fromIndex</span>. |
| 59 | + Returns -1 if the value is not found. |
| 60 | + </p> |
| 61 | + </div> |
| 62 | + |
| 63 | + <div class="Section"> |
| 64 | + <h2 class="Section-title">Proposal</h2> |
| 65 | + <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 |
| 68 | + </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 | + |
| 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> |
| 81 | + |
| 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 | + <span class="Code-punctuation">}</span> |
| 85 | + </code> |
| 86 | + </pre> |
| 87 | + </figure> |
| 88 | + </div> |
| 89 | + </main> |
52 | 90 |
|
53 | 91 | <script src="src/indexOf.js"></script>
|
54 | 92 | <script src="src/index.js"></script>
|
|
0 commit comments