aepstar commited on
Commit
5bb27fa
·
1 Parent(s): 87a0bc4
Files changed (2) hide show
  1. make_negative.html +532 -0
  2. positive_sum.html +542 -0
make_negative.html ADDED
@@ -0,0 +1,532 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
3
+
4
+ <meta charset="utf-8">
5
+ <meta name="generator" content="quarto-1.3.433">
6
+
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
8
+
9
+
10
+ <title>Codewars Challenge 2024 Menggunakan R dan RStudio - 5&nbsp; Return Negative</title>
11
+ <style>
12
+ code{white-space: pre-wrap;}
13
+ span.smallcaps{font-variant: small-caps;}
14
+ div.columns{display: flex; gap: min(4vw, 1.5em);}
15
+ div.column{flex: auto; overflow-x: auto;}
16
+ div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
17
+ ul.task-list{list-style: none;}
18
+ ul.task-list li input[type="checkbox"] {
19
+ width: 0.8em;
20
+ margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
21
+ vertical-align: middle;
22
+ }
23
+ /* CSS for syntax highlighting */
24
+ pre > code.sourceCode { white-space: pre; position: relative; }
25
+ pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
26
+ pre > code.sourceCode > span:empty { height: 1.2em; }
27
+ .sourceCode { overflow: visible; }
28
+ code.sourceCode > span { color: inherit; text-decoration: inherit; }
29
+ div.sourceCode { margin: 1em 0; }
30
+ pre.sourceCode { margin: 0; }
31
+ @media screen {
32
+ div.sourceCode { overflow: auto; }
33
+ }
34
+ @media print {
35
+ pre > code.sourceCode { white-space: pre-wrap; }
36
+ pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
37
+ }
38
+ pre.numberSource code
39
+ { counter-reset: source-line 0; }
40
+ pre.numberSource code > span
41
+ { position: relative; left: -4em; counter-increment: source-line; }
42
+ pre.numberSource code > span > a:first-child::before
43
+ { content: counter(source-line);
44
+ position: relative; left: -1em; text-align: right; vertical-align: baseline;
45
+ border: none; display: inline-block;
46
+ -webkit-touch-callout: none; -webkit-user-select: none;
47
+ -khtml-user-select: none; -moz-user-select: none;
48
+ -ms-user-select: none; user-select: none;
49
+ padding: 0 4px; width: 4em;
50
+ }
51
+ pre.numberSource { margin-left: 3em; padding-left: 4px; }
52
+ div.sourceCode
53
+ { }
54
+ @media screen {
55
+ pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
56
+ }
57
+ </style>
58
+
59
+
60
+ <script src="site_libs/quarto-nav/quarto-nav.js"></script>
61
+ <script src="site_libs/quarto-nav/headroom.min.js"></script>
62
+ <script src="site_libs/clipboard/clipboard.min.js"></script>
63
+ <script src="site_libs/quarto-search/autocomplete.umd.js"></script>
64
+ <script src="site_libs/quarto-search/fuse.min.js"></script>
65
+ <script src="site_libs/quarto-search/quarto-search.js"></script>
66
+ <meta name="quarto:offset" content="./">
67
+ <link href="./positive_sum.html" rel="next">
68
+ <link href="./reducegrow.html" rel="prev">
69
+ <script src="site_libs/quarto-html/quarto.js"></script>
70
+ <script src="site_libs/quarto-html/popper.min.js"></script>
71
+ <script src="site_libs/quarto-html/tippy.umd.min.js"></script>
72
+ <script src="site_libs/quarto-html/anchor.min.js"></script>
73
+ <link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
74
+ <link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
75
+ <script src="site_libs/bootstrap/bootstrap.min.js"></script>
76
+ <link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
77
+ <link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
78
+ <link href="site_libs/quarto-contrib/badge/badge.css" rel="stylesheet">
79
+ <script id="quarto-search-options" type="application/json">{
80
+ "location": "sidebar",
81
+ "copy-button": false,
82
+ "collapse-after": 3,
83
+ "panel-placement": "start",
84
+ "type": "textbox",
85
+ "limit": 20,
86
+ "language": {
87
+ "search-no-results-text": "No results",
88
+ "search-matching-documents-text": "matching documents",
89
+ "search-copy-link-title": "Copy link to search",
90
+ "search-hide-matches-text": "Hide additional matches",
91
+ "search-more-match-text": "more match in this document",
92
+ "search-more-matches-text": "more matches in this document",
93
+ "search-clear-button-title": "Clear",
94
+ "search-detached-cancel-button-title": "Cancel",
95
+ "search-submit-button-title": "Submit",
96
+ "search-label": "Search"
97
+ }
98
+ }</script>
99
+
100
+
101
+ </head>
102
+
103
+ <body class="nav-sidebar floating">
104
+
105
+ <div id="quarto-search-results"></div>
106
+ <header id="quarto-header" class="headroom fixed-top">
107
+ <nav class="quarto-secondary-nav">
108
+ <div class="container-fluid d-flex">
109
+ <button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
110
+ <i class="bi bi-layout-text-sidebar-reverse"></i>
111
+ </button>
112
+ <nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./make_negative.html"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Return Negative</span></a></li></ol></nav>
113
+ <a class="flex-grow-1" role="button" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
114
+ </a>
115
+ <button type="button" class="btn quarto-search-button" aria-label="" onclick="window.quartoOpenSearch();">
116
+ <i class="bi bi-search"></i>
117
+ </button>
118
+ </div>
119
+ </nav>
120
+ </header>
121
+ <!-- content -->
122
+ <div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
123
+ <!-- sidebar -->
124
+ <nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal sidebar-navigation floating overflow-auto">
125
+ <div class="pt-lg-2 mt-2 text-left sidebar-header">
126
+ <div class="sidebar-title mb-0 py-0">
127
+ <a href="./">Codewars Challenge 2024 Menggunakan R dan RStudio</a>
128
+ </div>
129
+ </div>
130
+ <div class="mt-2 flex-shrink-0 align-items-center">
131
+ <div class="sidebar-search">
132
+ <div id="quarto-search" class="" title="Search"></div>
133
+ </div>
134
+ </div>
135
+ <div class="sidebar-menu-container">
136
+ <ul class="list-unstyled mt-1">
137
+ <li class="sidebar-item">
138
+ <div class="sidebar-item-container">
139
+ <a href="./index.html" class="sidebar-item-text sidebar-link">
140
+ <span class="menu-text">Pengantar</span></a>
141
+ </div>
142
+ </li>
143
+ <li class="sidebar-item">
144
+ <div class="sidebar-item-container">
145
+ <a href="./evenodd.html" class="sidebar-item-text sidebar-link">
146
+ <span class="menu-text"><span class="chapter-number">1</span>&nbsp; <span class="chapter-title">Even or Odd</span></span></a>
147
+ </div>
148
+ </li>
149
+ <li class="sidebar-item">
150
+ <div class="sidebar-item-container">
151
+ <a href="./multiply.html" class="sidebar-item-text sidebar-link">
152
+ <span class="menu-text"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Multiply</span></span></a>
153
+ </div>
154
+ </li>
155
+ <li class="sidebar-item">
156
+ <div class="sidebar-item-container">
157
+ <a href="./countsheep.html" class="sidebar-item-text sidebar-link">
158
+ <span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">If you can’t sleep, just count sheep!!</span></span></a>
159
+ </div>
160
+ </li>
161
+ <li class="sidebar-item">
162
+ <div class="sidebar-item-container">
163
+ <a href="./reducegrow.html" class="sidebar-item-text sidebar-link">
164
+ <span class="menu-text"><span class="chapter-number">4</span>&nbsp; <span class="chapter-title">Beginner - Reduce but Grow</span></span></a>
165
+ </div>
166
+ </li>
167
+ <li class="sidebar-item">
168
+ <div class="sidebar-item-container">
169
+ <a href="./make_negative.html" class="sidebar-item-text sidebar-link active">
170
+ <span class="menu-text"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Return Negative</span></span></a>
171
+ </div>
172
+ </li>
173
+ <li class="sidebar-item">
174
+ <div class="sidebar-item-container">
175
+ <a href="./positive_sum.html" class="sidebar-item-text sidebar-link">
176
+ <span class="menu-text"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Sum of positive</span></span></a>
177
+ </div>
178
+ </li>
179
+ <li class="sidebar-item">
180
+ <div class="sidebar-item-container">
181
+ <a href="./references.html" class="sidebar-item-text sidebar-link">
182
+ <span class="menu-text">References</span></a>
183
+ </div>
184
+ </li>
185
+ </ul>
186
+ </div>
187
+ </nav>
188
+ <div id="quarto-sidebar-glass" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass"></div>
189
+ <!-- margin-sidebar -->
190
+ <div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
191
+ <nav id="TOC" role="doc-toc" class="toc-active">
192
+ <h2 id="toc-title">Table of contents</h2>
193
+
194
+ <ul>
195
+ <li><a href="#instruction" id="toc-instruction" class="nav-link active" data-scroll-target="#instruction"><span class="header-section-number">5.1</span> Instruction</a></li>
196
+ <li><a href="#youtube-video" id="toc-youtube-video" class="nav-link" data-scroll-target="#youtube-video"><span class="header-section-number">5.2</span> YouTube Video</a></li>
197
+ <li><a href="#solution-code" id="toc-solution-code" class="nav-link" data-scroll-target="#solution-code"><span class="header-section-number">5.3</span> Solution Code</a></li>
198
+ <li><a href="#test" id="toc-test" class="nav-link" data-scroll-target="#test"><span class="header-section-number">5.4</span> Test</a></li>
199
+ <li><a href="#supported-by" id="toc-supported-by" class="nav-link" data-scroll-target="#supported-by"><span class="header-section-number">5.5</span> Supported by</a></li>
200
+ </ul>
201
+ </nav>
202
+ </div>
203
+ <!-- main -->
204
+ <main class="content" id="quarto-document-content">
205
+
206
+ <header id="title-block-header" class="quarto-title-block default">
207
+ <div class="quarto-title">
208
+ <h1 class="title"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Return Negative</span></h1>
209
+ </div>
210
+
211
+
212
+
213
+ <div class="quarto-title-meta">
214
+
215
+
216
+
217
+
218
+ </div>
219
+
220
+
221
+ </header>
222
+
223
+ <p>Tantangan #5/366 - 18 Feb 2024</p>
224
+ <p><a href="https://www.codewars.com/kata/55685cd7ad70877c23000102/train/r" class="uri">https://www.codewars.com/kata/55685cd7ad70877c23000102/train/r</a></p>
225
+ <p><span class="badge rounded-pill quarto-badge " style="background-color: grey;">8kyu</span> Return Negative</p>
226
+ <section id="instruction" class="level2" data-number="5.1">
227
+ <h2 data-number="5.1" class="anchored" data-anchor-id="instruction"><span class="header-section-number">5.1</span> Instruction</h2>
228
+ <p>In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?</p>
229
+ <p>Examples</p>
230
+ <pre><code>make_negative(1); # return -1
231
+ make_negative(-5); # return -5
232
+ make_negative(0); # return 0</code></pre>
233
+ <p><strong>Notes</strong><br>
234
+ The number can be negative already, in which case no change is required. Zero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.</p>
235
+ </section>
236
+ <section id="youtube-video" class="level2" data-number="5.2">
237
+ <h2 data-number="5.2" class="anchored" data-anchor-id="youtube-video"><span class="header-section-number">5.2</span> YouTube Video</h2>
238
+ <div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/L8aczEhqymk?si=DSx84acpNvbkucrK" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
239
+ </section>
240
+ <section id="solution-code" class="level2" data-number="5.3">
241
+ <h2 data-number="5.3" class="anchored" data-anchor-id="solution-code"><span class="header-section-number">5.3</span> Solution Code</h2>
242
+ <p>Solusi bar-bar</p>
243
+ <div class="cell">
244
+ <div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>make_negative <span class="ot">&lt;-</span> <span class="cf">function</span>(num){</span>
245
+ <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ifelse</span>(num <span class="sc">&lt;=</span> <span class="dv">0</span>, num, <span class="sc">-</span><span class="dv">1</span><span class="sc">*</span>num)</span>
246
+ <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
247
+ </div>
248
+ <p>Solusi simple</p>
249
+ <div class="cell">
250
+ <div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>make_negative <span class="ot">&lt;-</span> <span class="cf">function</span>(num)<span class="sc">-</span><span class="fu">abs</span>(num)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
251
+ </div>
252
+ </section>
253
+ <section id="test" class="level2" data-number="5.4">
254
+ <h2 data-number="5.4" class="anchored" data-anchor-id="test"><span class="header-section-number">5.4</span> Test</h2>
255
+ <div class="cell">
256
+ <div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(testthat)</span>
257
+ <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
258
+ <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="fu">test_that</span>(<span class="st">'Basic tests'</span>, {</span>
259
+ <span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">make_negative</span>(<span class="dv">42</span>), <span class="sc">-</span><span class="dv">42</span>)</span>
260
+ <span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">make_negative</span>(<span class="sc">-</span><span class="dv">9</span>), <span class="sc">-</span><span class="dv">9</span>)</span>
261
+ <span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">make_negative</span>(<span class="dv">0</span>), <span class="dv">0</span>)</span>
262
+ <span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">make_negative</span>(<span class="dv">1</span>), <span class="sc">-</span><span class="dv">1</span>)</span>
263
+ <span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">make_negative</span>(<span class="sc">-</span><span class="dv">1</span>), <span class="sc">-</span><span class="dv">1</span>)</span>
264
+ <span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a>})</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
265
+ <div class="cell-output cell-output-stdout">
266
+ <pre><code>Test passed 🎊</code></pre>
267
+ </div>
268
+ </div>
269
+ </section>
270
+ <section id="supported-by" class="level2" data-number="5.5">
271
+ <h2 data-number="5.5" class="anchored" data-anchor-id="supported-by"><span class="header-section-number">5.5</span> Supported by</h2>
272
+ <div class="quarto-figure quarto-figure-center">
273
+ <figure class="figure">
274
+ <p><a href="https://www.starcore.co"><img src="new_starcore_logo.png" class="img-fluid figure-img"></a></p>
275
+ <figcaption class="figure-caption">StarCore Analytics</figcaption>
276
+ </figure>
277
+ </div>
278
+
279
+
280
+ </section>
281
+
282
+ </main> <!-- /main -->
283
+ <script id="quarto-html-after-body" type="application/javascript">
284
+ window.document.addEventListener("DOMContentLoaded", function (event) {
285
+ const toggleBodyColorMode = (bsSheetEl) => {
286
+ const mode = bsSheetEl.getAttribute("data-mode");
287
+ const bodyEl = window.document.querySelector("body");
288
+ if (mode === "dark") {
289
+ bodyEl.classList.add("quarto-dark");
290
+ bodyEl.classList.remove("quarto-light");
291
+ } else {
292
+ bodyEl.classList.add("quarto-light");
293
+ bodyEl.classList.remove("quarto-dark");
294
+ }
295
+ }
296
+ const toggleBodyColorPrimary = () => {
297
+ const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
298
+ if (bsSheetEl) {
299
+ toggleBodyColorMode(bsSheetEl);
300
+ }
301
+ }
302
+ toggleBodyColorPrimary();
303
+ const icon = "";
304
+ const anchorJS = new window.AnchorJS();
305
+ anchorJS.options = {
306
+ placement: 'right',
307
+ icon: icon
308
+ };
309
+ anchorJS.add('.anchored');
310
+ const isCodeAnnotation = (el) => {
311
+ for (const clz of el.classList) {
312
+ if (clz.startsWith('code-annotation-')) {
313
+ return true;
314
+ }
315
+ }
316
+ return false;
317
+ }
318
+ const clipboard = new window.ClipboardJS('.code-copy-button', {
319
+ text: function(trigger) {
320
+ const codeEl = trigger.previousElementSibling.cloneNode(true);
321
+ for (const childEl of codeEl.children) {
322
+ if (isCodeAnnotation(childEl)) {
323
+ childEl.remove();
324
+ }
325
+ }
326
+ return codeEl.innerText;
327
+ }
328
+ });
329
+ clipboard.on('success', function(e) {
330
+ // button target
331
+ const button = e.trigger;
332
+ // don't keep focus
333
+ button.blur();
334
+ // flash "checked"
335
+ button.classList.add('code-copy-button-checked');
336
+ var currentTitle = button.getAttribute("title");
337
+ button.setAttribute("title", "Copied!");
338
+ let tooltip;
339
+ if (window.bootstrap) {
340
+ button.setAttribute("data-bs-toggle", "tooltip");
341
+ button.setAttribute("data-bs-placement", "left");
342
+ button.setAttribute("data-bs-title", "Copied!");
343
+ tooltip = new bootstrap.Tooltip(button,
344
+ { trigger: "manual",
345
+ customClass: "code-copy-button-tooltip",
346
+ offset: [0, -8]});
347
+ tooltip.show();
348
+ }
349
+ setTimeout(function() {
350
+ if (tooltip) {
351
+ tooltip.hide();
352
+ button.removeAttribute("data-bs-title");
353
+ button.removeAttribute("data-bs-toggle");
354
+ button.removeAttribute("data-bs-placement");
355
+ }
356
+ button.setAttribute("title", currentTitle);
357
+ button.classList.remove('code-copy-button-checked');
358
+ }, 1000);
359
+ // clear code selection
360
+ e.clearSelection();
361
+ });
362
+ function tippyHover(el, contentFn) {
363
+ const config = {
364
+ allowHTML: true,
365
+ content: contentFn,
366
+ maxWidth: 500,
367
+ delay: 100,
368
+ arrow: false,
369
+ appendTo: function(el) {
370
+ return el.parentElement;
371
+ },
372
+ interactive: true,
373
+ interactiveBorder: 10,
374
+ theme: 'quarto',
375
+ placement: 'bottom-start'
376
+ };
377
+ window.tippy(el, config);
378
+ }
379
+ const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
380
+ for (var i=0; i<noterefs.length; i++) {
381
+ const ref = noterefs[i];
382
+ tippyHover(ref, function() {
383
+ // use id or data attribute instead here
384
+ let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
385
+ try { href = new URL(href).hash; } catch {}
386
+ const id = href.replace(/^#\/?/, "");
387
+ const note = window.document.getElementById(id);
388
+ return note.innerHTML;
389
+ });
390
+ }
391
+ let selectedAnnoteEl;
392
+ const selectorForAnnotation = ( cell, annotation) => {
393
+ let cellAttr = 'data-code-cell="' + cell + '"';
394
+ let lineAttr = 'data-code-annotation="' + annotation + '"';
395
+ const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
396
+ return selector;
397
+ }
398
+ const selectCodeLines = (annoteEl) => {
399
+ const doc = window.document;
400
+ const targetCell = annoteEl.getAttribute("data-target-cell");
401
+ const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
402
+ const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
403
+ const lines = annoteSpan.getAttribute("data-code-lines").split(",");
404
+ const lineIds = lines.map((line) => {
405
+ return targetCell + "-" + line;
406
+ })
407
+ let top = null;
408
+ let height = null;
409
+ let parent = null;
410
+ if (lineIds.length > 0) {
411
+ //compute the position of the single el (top and bottom and make a div)
412
+ const el = window.document.getElementById(lineIds[0]);
413
+ top = el.offsetTop;
414
+ height = el.offsetHeight;
415
+ parent = el.parentElement.parentElement;
416
+ if (lineIds.length > 1) {
417
+ const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
418
+ const bottom = lastEl.offsetTop + lastEl.offsetHeight;
419
+ height = bottom - top;
420
+ }
421
+ if (top !== null && height !== null && parent !== null) {
422
+ // cook up a div (if necessary) and position it
423
+ let div = window.document.getElementById("code-annotation-line-highlight");
424
+ if (div === null) {
425
+ div = window.document.createElement("div");
426
+ div.setAttribute("id", "code-annotation-line-highlight");
427
+ div.style.position = 'absolute';
428
+ parent.appendChild(div);
429
+ }
430
+ div.style.top = top - 2 + "px";
431
+ div.style.height = height + 4 + "px";
432
+ let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
433
+ if (gutterDiv === null) {
434
+ gutterDiv = window.document.createElement("div");
435
+ gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
436
+ gutterDiv.style.position = 'absolute';
437
+ const codeCell = window.document.getElementById(targetCell);
438
+ const gutter = codeCell.querySelector('.code-annotation-gutter');
439
+ gutter.appendChild(gutterDiv);
440
+ }
441
+ gutterDiv.style.top = top - 2 + "px";
442
+ gutterDiv.style.height = height + 4 + "px";
443
+ }
444
+ selectedAnnoteEl = annoteEl;
445
+ }
446
+ };
447
+ const unselectCodeLines = () => {
448
+ const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
449
+ elementsIds.forEach((elId) => {
450
+ const div = window.document.getElementById(elId);
451
+ if (div) {
452
+ div.remove();
453
+ }
454
+ });
455
+ selectedAnnoteEl = undefined;
456
+ };
457
+ // Attach click handler to the DT
458
+ const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
459
+ for (const annoteDlNode of annoteDls) {
460
+ annoteDlNode.addEventListener('click', (event) => {
461
+ const clickedEl = event.target;
462
+ if (clickedEl !== selectedAnnoteEl) {
463
+ unselectCodeLines();
464
+ const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
465
+ if (activeEl) {
466
+ activeEl.classList.remove('code-annotation-active');
467
+ }
468
+ selectCodeLines(clickedEl);
469
+ clickedEl.classList.add('code-annotation-active');
470
+ } else {
471
+ // Unselect the line
472
+ unselectCodeLines();
473
+ clickedEl.classList.remove('code-annotation-active');
474
+ }
475
+ });
476
+ }
477
+ const findCites = (el) => {
478
+ const parentEl = el.parentElement;
479
+ if (parentEl) {
480
+ const cites = parentEl.dataset.cites;
481
+ if (cites) {
482
+ return {
483
+ el,
484
+ cites: cites.split(' ')
485
+ };
486
+ } else {
487
+ return findCites(el.parentElement)
488
+ }
489
+ } else {
490
+ return undefined;
491
+ }
492
+ };
493
+ var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
494
+ for (var i=0; i<bibliorefs.length; i++) {
495
+ const ref = bibliorefs[i];
496
+ const citeInfo = findCites(ref);
497
+ if (citeInfo) {
498
+ tippyHover(citeInfo.el, function() {
499
+ var popup = window.document.createElement('div');
500
+ citeInfo.cites.forEach(function(cite) {
501
+ var citeDiv = window.document.createElement('div');
502
+ citeDiv.classList.add('hanging-indent');
503
+ citeDiv.classList.add('csl-entry');
504
+ var biblioDiv = window.document.getElementById('ref-' + cite);
505
+ if (biblioDiv) {
506
+ citeDiv.innerHTML = biblioDiv.innerHTML;
507
+ }
508
+ popup.appendChild(citeDiv);
509
+ });
510
+ return popup.innerHTML;
511
+ });
512
+ }
513
+ }
514
+ });
515
+ </script>
516
+ <nav class="page-navigation">
517
+ <div class="nav-page nav-page-previous">
518
+ <a href="./reducegrow.html" class="pagination-link">
519
+ <i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">4</span>&nbsp; <span class="chapter-title">Beginner - Reduce but Grow</span></span>
520
+ </a>
521
+ </div>
522
+ <div class="nav-page nav-page-next">
523
+ <a href="./positive_sum.html" class="pagination-link">
524
+ <span class="nav-page-text"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Sum of positive</span></span> <i class="bi bi-arrow-right-short"></i>
525
+ </a>
526
+ </div>
527
+ </nav>
528
+ </div> <!-- /content -->
529
+
530
+
531
+
532
+ </body></html>
positive_sum.html ADDED
@@ -0,0 +1,542 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
3
+
4
+ <meta charset="utf-8">
5
+ <meta name="generator" content="quarto-1.3.433">
6
+
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
8
+
9
+
10
+ <title>Codewars Challenge 2024 Menggunakan R dan RStudio - 6&nbsp; Sum of positive</title>
11
+ <style>
12
+ code{white-space: pre-wrap;}
13
+ span.smallcaps{font-variant: small-caps;}
14
+ div.columns{display: flex; gap: min(4vw, 1.5em);}
15
+ div.column{flex: auto; overflow-x: auto;}
16
+ div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
17
+ ul.task-list{list-style: none;}
18
+ ul.task-list li input[type="checkbox"] {
19
+ width: 0.8em;
20
+ margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
21
+ vertical-align: middle;
22
+ }
23
+ /* CSS for syntax highlighting */
24
+ pre > code.sourceCode { white-space: pre; position: relative; }
25
+ pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
26
+ pre > code.sourceCode > span:empty { height: 1.2em; }
27
+ .sourceCode { overflow: visible; }
28
+ code.sourceCode > span { color: inherit; text-decoration: inherit; }
29
+ div.sourceCode { margin: 1em 0; }
30
+ pre.sourceCode { margin: 0; }
31
+ @media screen {
32
+ div.sourceCode { overflow: auto; }
33
+ }
34
+ @media print {
35
+ pre > code.sourceCode { white-space: pre-wrap; }
36
+ pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
37
+ }
38
+ pre.numberSource code
39
+ { counter-reset: source-line 0; }
40
+ pre.numberSource code > span
41
+ { position: relative; left: -4em; counter-increment: source-line; }
42
+ pre.numberSource code > span > a:first-child::before
43
+ { content: counter(source-line);
44
+ position: relative; left: -1em; text-align: right; vertical-align: baseline;
45
+ border: none; display: inline-block;
46
+ -webkit-touch-callout: none; -webkit-user-select: none;
47
+ -khtml-user-select: none; -moz-user-select: none;
48
+ -ms-user-select: none; user-select: none;
49
+ padding: 0 4px; width: 4em;
50
+ }
51
+ pre.numberSource { margin-left: 3em; padding-left: 4px; }
52
+ div.sourceCode
53
+ { }
54
+ @media screen {
55
+ pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
56
+ }
57
+ </style>
58
+
59
+
60
+ <script src="site_libs/quarto-nav/quarto-nav.js"></script>
61
+ <script src="site_libs/quarto-nav/headroom.min.js"></script>
62
+ <script src="site_libs/clipboard/clipboard.min.js"></script>
63
+ <script src="site_libs/quarto-search/autocomplete.umd.js"></script>
64
+ <script src="site_libs/quarto-search/fuse.min.js"></script>
65
+ <script src="site_libs/quarto-search/quarto-search.js"></script>
66
+ <meta name="quarto:offset" content="./">
67
+ <link href="./references.html" rel="next">
68
+ <link href="./make_negative.html" rel="prev">
69
+ <script src="site_libs/quarto-html/quarto.js"></script>
70
+ <script src="site_libs/quarto-html/popper.min.js"></script>
71
+ <script src="site_libs/quarto-html/tippy.umd.min.js"></script>
72
+ <script src="site_libs/quarto-html/anchor.min.js"></script>
73
+ <link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
74
+ <link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
75
+ <script src="site_libs/bootstrap/bootstrap.min.js"></script>
76
+ <link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
77
+ <link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
78
+ <link href="site_libs/quarto-contrib/badge/badge.css" rel="stylesheet">
79
+ <script id="quarto-search-options" type="application/json">{
80
+ "location": "sidebar",
81
+ "copy-button": false,
82
+ "collapse-after": 3,
83
+ "panel-placement": "start",
84
+ "type": "textbox",
85
+ "limit": 20,
86
+ "language": {
87
+ "search-no-results-text": "No results",
88
+ "search-matching-documents-text": "matching documents",
89
+ "search-copy-link-title": "Copy link to search",
90
+ "search-hide-matches-text": "Hide additional matches",
91
+ "search-more-match-text": "more match in this document",
92
+ "search-more-matches-text": "more matches in this document",
93
+ "search-clear-button-title": "Clear",
94
+ "search-detached-cancel-button-title": "Cancel",
95
+ "search-submit-button-title": "Submit",
96
+ "search-label": "Search"
97
+ }
98
+ }</script>
99
+
100
+
101
+ </head>
102
+
103
+ <body class="nav-sidebar floating">
104
+
105
+ <div id="quarto-search-results"></div>
106
+ <header id="quarto-header" class="headroom fixed-top">
107
+ <nav class="quarto-secondary-nav">
108
+ <div class="container-fluid d-flex">
109
+ <button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
110
+ <i class="bi bi-layout-text-sidebar-reverse"></i>
111
+ </button>
112
+ <nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./positive_sum.html"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Sum of positive</span></a></li></ol></nav>
113
+ <a class="flex-grow-1" role="button" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
114
+ </a>
115
+ <button type="button" class="btn quarto-search-button" aria-label="" onclick="window.quartoOpenSearch();">
116
+ <i class="bi bi-search"></i>
117
+ </button>
118
+ </div>
119
+ </nav>
120
+ </header>
121
+ <!-- content -->
122
+ <div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
123
+ <!-- sidebar -->
124
+ <nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal sidebar-navigation floating overflow-auto">
125
+ <div class="pt-lg-2 mt-2 text-left sidebar-header">
126
+ <div class="sidebar-title mb-0 py-0">
127
+ <a href="./">Codewars Challenge 2024 Menggunakan R dan RStudio</a>
128
+ </div>
129
+ </div>
130
+ <div class="mt-2 flex-shrink-0 align-items-center">
131
+ <div class="sidebar-search">
132
+ <div id="quarto-search" class="" title="Search"></div>
133
+ </div>
134
+ </div>
135
+ <div class="sidebar-menu-container">
136
+ <ul class="list-unstyled mt-1">
137
+ <li class="sidebar-item">
138
+ <div class="sidebar-item-container">
139
+ <a href="./index.html" class="sidebar-item-text sidebar-link">
140
+ <span class="menu-text">Pengantar</span></a>
141
+ </div>
142
+ </li>
143
+ <li class="sidebar-item">
144
+ <div class="sidebar-item-container">
145
+ <a href="./evenodd.html" class="sidebar-item-text sidebar-link">
146
+ <span class="menu-text"><span class="chapter-number">1</span>&nbsp; <span class="chapter-title">Even or Odd</span></span></a>
147
+ </div>
148
+ </li>
149
+ <li class="sidebar-item">
150
+ <div class="sidebar-item-container">
151
+ <a href="./multiply.html" class="sidebar-item-text sidebar-link">
152
+ <span class="menu-text"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Multiply</span></span></a>
153
+ </div>
154
+ </li>
155
+ <li class="sidebar-item">
156
+ <div class="sidebar-item-container">
157
+ <a href="./countsheep.html" class="sidebar-item-text sidebar-link">
158
+ <span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">If you can’t sleep, just count sheep!!</span></span></a>
159
+ </div>
160
+ </li>
161
+ <li class="sidebar-item">
162
+ <div class="sidebar-item-container">
163
+ <a href="./reducegrow.html" class="sidebar-item-text sidebar-link">
164
+ <span class="menu-text"><span class="chapter-number">4</span>&nbsp; <span class="chapter-title">Beginner - Reduce but Grow</span></span></a>
165
+ </div>
166
+ </li>
167
+ <li class="sidebar-item">
168
+ <div class="sidebar-item-container">
169
+ <a href="./make_negative.html" class="sidebar-item-text sidebar-link">
170
+ <span class="menu-text"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Return Negative</span></span></a>
171
+ </div>
172
+ </li>
173
+ <li class="sidebar-item">
174
+ <div class="sidebar-item-container">
175
+ <a href="./positive_sum.html" class="sidebar-item-text sidebar-link active">
176
+ <span class="menu-text"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Sum of positive</span></span></a>
177
+ </div>
178
+ </li>
179
+ <li class="sidebar-item">
180
+ <div class="sidebar-item-container">
181
+ <a href="./references.html" class="sidebar-item-text sidebar-link">
182
+ <span class="menu-text">References</span></a>
183
+ </div>
184
+ </li>
185
+ </ul>
186
+ </div>
187
+ </nav>
188
+ <div id="quarto-sidebar-glass" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass"></div>
189
+ <!-- margin-sidebar -->
190
+ <div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
191
+ <nav id="TOC" role="doc-toc" class="toc-active">
192
+ <h2 id="toc-title">Table of contents</h2>
193
+
194
+ <ul>
195
+ <li><a href="#instruction" id="toc-instruction" class="nav-link active" data-scroll-target="#instruction"><span class="header-section-number">6.1</span> Instruction</a></li>
196
+ <li><a href="#youtube-video" id="toc-youtube-video" class="nav-link" data-scroll-target="#youtube-video"><span class="header-section-number">6.2</span> YouTube Video</a></li>
197
+ <li><a href="#solution-code" id="toc-solution-code" class="nav-link" data-scroll-target="#solution-code"><span class="header-section-number">6.3</span> Solution Code</a></li>
198
+ <li><a href="#test" id="toc-test" class="nav-link" data-scroll-target="#test"><span class="header-section-number">6.4</span> Test</a></li>
199
+ <li><a href="#supported-by" id="toc-supported-by" class="nav-link" data-scroll-target="#supported-by"><span class="header-section-number">6.5</span> Supported by</a></li>
200
+ </ul>
201
+ </nav>
202
+ </div>
203
+ <!-- main -->
204
+ <main class="content" id="quarto-document-content">
205
+
206
+ <header id="title-block-header" class="quarto-title-block default">
207
+ <div class="quarto-title">
208
+ <h1 class="title"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Sum of positive</span></h1>
209
+ </div>
210
+
211
+
212
+
213
+ <div class="quarto-title-meta">
214
+
215
+
216
+
217
+
218
+ </div>
219
+
220
+
221
+ </header>
222
+
223
+ <p>Tantangan #6/366 - 19 Feb 2024</p>
224
+ <p><a href="https://www.codewars.com/kata/5715eaedb436cf5606000381/train/r" class="uri">https://www.codewars.com/kata/5715eaedb436cf5606000381/train/r</a></p>
225
+ <p><span class="badge rounded-pill quarto-badge " style="background-color: grey;">8kyu</span> Sum of positive</p>
226
+ <section id="instruction" class="level2" data-number="6.1">
227
+ <h2 data-number="6.1" class="anchored" data-anchor-id="instruction"><span class="header-section-number">6.1</span> Instruction</h2>
228
+ <p>You get an array of numbers, return the sum of all of the positives ones.</p>
229
+ <p>Example <code>[1,-4,7,12]</code> =&gt; <code>1 + 7 + 12 = 20</code></p>
230
+ <p>Note: if there is nothing to sum, the sum is default to 0.</p>
231
+ </section>
232
+ <section id="youtube-video" class="level2" data-number="6.2">
233
+ <h2 data-number="6.2" class="anchored" data-anchor-id="youtube-video"><span class="header-section-number">6.2</span> YouTube Video</h2>
234
+ <div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/xCvHwR52p4k?si=yoLfpHfcIBYk0j9c" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
235
+ </section>
236
+ <section id="solution-code" class="level2" data-number="6.3">
237
+ <h2 data-number="6.3" class="anchored" data-anchor-id="solution-code"><span class="header-section-number">6.3</span> Solution Code</h2>
238
+ <p>Solusi bar-bar</p>
239
+ <div class="cell">
240
+ <div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>positive_sum <span class="ot">&lt;-</span> <span class="cf">function</span>(vec){</span>
241
+ <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> pos_vec <span class="ot">&lt;-</span> <span class="fu">c</span>()</span>
242
+ <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> vec){</span>
243
+ <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(i <span class="sc">&gt;</span> <span class="dv">0</span>){</span>
244
+ <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> pos_vec <span class="ot">&lt;-</span> <span class="fu">c</span>(pos_vec, i)</span>
245
+ <span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
246
+ <span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> pos_vec <span class="ot">&lt;-</span> <span class="fu">c</span>(pos_vec, <span class="dv">0</span>)</span>
247
+ <span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> } </span>
248
+ <span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> }</span>
249
+ <span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">sum</span>(pos_vec))</span>
250
+ <span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
251
+ </div>
252
+ <p>Solusi simple</p>
253
+ <div class="cell">
254
+ <div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>positive_sum <span class="ot">&lt;-</span> <span class="cf">function</span>(vec){</span>
255
+ <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> vec[vec <span class="sc">&lt;</span> <span class="dv">0</span>] <span class="ot">&lt;-</span> <span class="dv">0</span></span>
256
+ <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">sum</span>(vec)</span>
257
+ <span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
258
+ </div>
259
+ <div class="cell">
260
+ <div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>positive_sum <span class="ot">&lt;-</span> <span class="cf">function</span>(vec)<span class="fu">sum</span>(vec[vec<span class="sc">&gt;</span><span class="dv">0</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
261
+ </div>
262
+ </section>
263
+ <section id="test" class="level2" data-number="6.4">
264
+ <h2 data-number="6.4" class="anchored" data-anchor-id="test"><span class="header-section-number">6.4</span> Test</h2>
265
+ <div class="cell">
266
+ <div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(testthat)</span>
267
+ <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a></span>
268
+ <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="fu">test_that</span>(<span class="st">'Basic tests'</span>, {</span>
269
+ <span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">positive_sum</span>(<span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>)),<span class="dv">15</span>)</span>
270
+ <span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">positive_sum</span>(<span class="fu">c</span>(<span class="dv">1</span>,<span class="sc">-</span><span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>)),<span class="dv">13</span>)</span>
271
+ <span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">positive_sum</span>(<span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="sc">-</span><span class="dv">5</span>)),<span class="dv">9</span>)</span>
272
+ <span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">positive_sum</span>(<span class="fu">c</span>()),<span class="dv">0</span>)</span>
273
+ <span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">positive_sum</span>(<span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>,<span class="sc">-</span><span class="dv">2</span>,<span class="sc">-</span><span class="dv">3</span>,<span class="sc">-</span><span class="dv">4</span>,<span class="sc">-</span><span class="dv">5</span>)),<span class="dv">0</span>)</span>
274
+ <span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a>})</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
275
+ <div class="cell-output cell-output-stdout">
276
+ <pre><code>Test passed 🎊</code></pre>
277
+ </div>
278
+ </div>
279
+ </section>
280
+ <section id="supported-by" class="level2" data-number="6.5">
281
+ <h2 data-number="6.5" class="anchored" data-anchor-id="supported-by"><span class="header-section-number">6.5</span> Supported by</h2>
282
+ <div class="quarto-figure quarto-figure-center">
283
+ <figure class="figure">
284
+ <p><a href="https://www.starcore.co"><img src="new_starcore_logo.png" class="img-fluid figure-img"></a></p>
285
+ <figcaption class="figure-caption">StarCore Analytics</figcaption>
286
+ </figure>
287
+ </div>
288
+
289
+
290
+ </section>
291
+
292
+ </main> <!-- /main -->
293
+ <script id="quarto-html-after-body" type="application/javascript">
294
+ window.document.addEventListener("DOMContentLoaded", function (event) {
295
+ const toggleBodyColorMode = (bsSheetEl) => {
296
+ const mode = bsSheetEl.getAttribute("data-mode");
297
+ const bodyEl = window.document.querySelector("body");
298
+ if (mode === "dark") {
299
+ bodyEl.classList.add("quarto-dark");
300
+ bodyEl.classList.remove("quarto-light");
301
+ } else {
302
+ bodyEl.classList.add("quarto-light");
303
+ bodyEl.classList.remove("quarto-dark");
304
+ }
305
+ }
306
+ const toggleBodyColorPrimary = () => {
307
+ const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
308
+ if (bsSheetEl) {
309
+ toggleBodyColorMode(bsSheetEl);
310
+ }
311
+ }
312
+ toggleBodyColorPrimary();
313
+ const icon = "";
314
+ const anchorJS = new window.AnchorJS();
315
+ anchorJS.options = {
316
+ placement: 'right',
317
+ icon: icon
318
+ };
319
+ anchorJS.add('.anchored');
320
+ const isCodeAnnotation = (el) => {
321
+ for (const clz of el.classList) {
322
+ if (clz.startsWith('code-annotation-')) {
323
+ return true;
324
+ }
325
+ }
326
+ return false;
327
+ }
328
+ const clipboard = new window.ClipboardJS('.code-copy-button', {
329
+ text: function(trigger) {
330
+ const codeEl = trigger.previousElementSibling.cloneNode(true);
331
+ for (const childEl of codeEl.children) {
332
+ if (isCodeAnnotation(childEl)) {
333
+ childEl.remove();
334
+ }
335
+ }
336
+ return codeEl.innerText;
337
+ }
338
+ });
339
+ clipboard.on('success', function(e) {
340
+ // button target
341
+ const button = e.trigger;
342
+ // don't keep focus
343
+ button.blur();
344
+ // flash "checked"
345
+ button.classList.add('code-copy-button-checked');
346
+ var currentTitle = button.getAttribute("title");
347
+ button.setAttribute("title", "Copied!");
348
+ let tooltip;
349
+ if (window.bootstrap) {
350
+ button.setAttribute("data-bs-toggle", "tooltip");
351
+ button.setAttribute("data-bs-placement", "left");
352
+ button.setAttribute("data-bs-title", "Copied!");
353
+ tooltip = new bootstrap.Tooltip(button,
354
+ { trigger: "manual",
355
+ customClass: "code-copy-button-tooltip",
356
+ offset: [0, -8]});
357
+ tooltip.show();
358
+ }
359
+ setTimeout(function() {
360
+ if (tooltip) {
361
+ tooltip.hide();
362
+ button.removeAttribute("data-bs-title");
363
+ button.removeAttribute("data-bs-toggle");
364
+ button.removeAttribute("data-bs-placement");
365
+ }
366
+ button.setAttribute("title", currentTitle);
367
+ button.classList.remove('code-copy-button-checked');
368
+ }, 1000);
369
+ // clear code selection
370
+ e.clearSelection();
371
+ });
372
+ function tippyHover(el, contentFn) {
373
+ const config = {
374
+ allowHTML: true,
375
+ content: contentFn,
376
+ maxWidth: 500,
377
+ delay: 100,
378
+ arrow: false,
379
+ appendTo: function(el) {
380
+ return el.parentElement;
381
+ },
382
+ interactive: true,
383
+ interactiveBorder: 10,
384
+ theme: 'quarto',
385
+ placement: 'bottom-start'
386
+ };
387
+ window.tippy(el, config);
388
+ }
389
+ const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
390
+ for (var i=0; i<noterefs.length; i++) {
391
+ const ref = noterefs[i];
392
+ tippyHover(ref, function() {
393
+ // use id or data attribute instead here
394
+ let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
395
+ try { href = new URL(href).hash; } catch {}
396
+ const id = href.replace(/^#\/?/, "");
397
+ const note = window.document.getElementById(id);
398
+ return note.innerHTML;
399
+ });
400
+ }
401
+ let selectedAnnoteEl;
402
+ const selectorForAnnotation = ( cell, annotation) => {
403
+ let cellAttr = 'data-code-cell="' + cell + '"';
404
+ let lineAttr = 'data-code-annotation="' + annotation + '"';
405
+ const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
406
+ return selector;
407
+ }
408
+ const selectCodeLines = (annoteEl) => {
409
+ const doc = window.document;
410
+ const targetCell = annoteEl.getAttribute("data-target-cell");
411
+ const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
412
+ const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
413
+ const lines = annoteSpan.getAttribute("data-code-lines").split(",");
414
+ const lineIds = lines.map((line) => {
415
+ return targetCell + "-" + line;
416
+ })
417
+ let top = null;
418
+ let height = null;
419
+ let parent = null;
420
+ if (lineIds.length > 0) {
421
+ //compute the position of the single el (top and bottom and make a div)
422
+ const el = window.document.getElementById(lineIds[0]);
423
+ top = el.offsetTop;
424
+ height = el.offsetHeight;
425
+ parent = el.parentElement.parentElement;
426
+ if (lineIds.length > 1) {
427
+ const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
428
+ const bottom = lastEl.offsetTop + lastEl.offsetHeight;
429
+ height = bottom - top;
430
+ }
431
+ if (top !== null && height !== null && parent !== null) {
432
+ // cook up a div (if necessary) and position it
433
+ let div = window.document.getElementById("code-annotation-line-highlight");
434
+ if (div === null) {
435
+ div = window.document.createElement("div");
436
+ div.setAttribute("id", "code-annotation-line-highlight");
437
+ div.style.position = 'absolute';
438
+ parent.appendChild(div);
439
+ }
440
+ div.style.top = top - 2 + "px";
441
+ div.style.height = height + 4 + "px";
442
+ let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
443
+ if (gutterDiv === null) {
444
+ gutterDiv = window.document.createElement("div");
445
+ gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
446
+ gutterDiv.style.position = 'absolute';
447
+ const codeCell = window.document.getElementById(targetCell);
448
+ const gutter = codeCell.querySelector('.code-annotation-gutter');
449
+ gutter.appendChild(gutterDiv);
450
+ }
451
+ gutterDiv.style.top = top - 2 + "px";
452
+ gutterDiv.style.height = height + 4 + "px";
453
+ }
454
+ selectedAnnoteEl = annoteEl;
455
+ }
456
+ };
457
+ const unselectCodeLines = () => {
458
+ const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
459
+ elementsIds.forEach((elId) => {
460
+ const div = window.document.getElementById(elId);
461
+ if (div) {
462
+ div.remove();
463
+ }
464
+ });
465
+ selectedAnnoteEl = undefined;
466
+ };
467
+ // Attach click handler to the DT
468
+ const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
469
+ for (const annoteDlNode of annoteDls) {
470
+ annoteDlNode.addEventListener('click', (event) => {
471
+ const clickedEl = event.target;
472
+ if (clickedEl !== selectedAnnoteEl) {
473
+ unselectCodeLines();
474
+ const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
475
+ if (activeEl) {
476
+ activeEl.classList.remove('code-annotation-active');
477
+ }
478
+ selectCodeLines(clickedEl);
479
+ clickedEl.classList.add('code-annotation-active');
480
+ } else {
481
+ // Unselect the line
482
+ unselectCodeLines();
483
+ clickedEl.classList.remove('code-annotation-active');
484
+ }
485
+ });
486
+ }
487
+ const findCites = (el) => {
488
+ const parentEl = el.parentElement;
489
+ if (parentEl) {
490
+ const cites = parentEl.dataset.cites;
491
+ if (cites) {
492
+ return {
493
+ el,
494
+ cites: cites.split(' ')
495
+ };
496
+ } else {
497
+ return findCites(el.parentElement)
498
+ }
499
+ } else {
500
+ return undefined;
501
+ }
502
+ };
503
+ var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
504
+ for (var i=0; i<bibliorefs.length; i++) {
505
+ const ref = bibliorefs[i];
506
+ const citeInfo = findCites(ref);
507
+ if (citeInfo) {
508
+ tippyHover(citeInfo.el, function() {
509
+ var popup = window.document.createElement('div');
510
+ citeInfo.cites.forEach(function(cite) {
511
+ var citeDiv = window.document.createElement('div');
512
+ citeDiv.classList.add('hanging-indent');
513
+ citeDiv.classList.add('csl-entry');
514
+ var biblioDiv = window.document.getElementById('ref-' + cite);
515
+ if (biblioDiv) {
516
+ citeDiv.innerHTML = biblioDiv.innerHTML;
517
+ }
518
+ popup.appendChild(citeDiv);
519
+ });
520
+ return popup.innerHTML;
521
+ });
522
+ }
523
+ }
524
+ });
525
+ </script>
526
+ <nav class="page-navigation">
527
+ <div class="nav-page nav-page-previous">
528
+ <a href="./make_negative.html" class="pagination-link">
529
+ <i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Return Negative</span></span>
530
+ </a>
531
+ </div>
532
+ <div class="nav-page nav-page-next">
533
+ <a href="./references.html" class="pagination-link">
534
+ <span class="nav-page-text">References</span> <i class="bi bi-arrow-right-short"></i>
535
+ </a>
536
+ </div>
537
+ </nav>
538
+ </div> <!-- /content -->
539
+
540
+
541
+
542
+ </body></html>