avatarMonu Kumar Modi

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1612

Abstract

en = str.<span class="hljs-property">length</span> <span class="hljs-keyword">let</span> chunk1, chunk2 <span class="hljs-keyword">let</span> chunkStr = <span class="hljs-string">""</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < strLen; i += k) { <span class="hljs-keyword">if</span> (i % (<span class="hljs-number">2</span>*k) == <span class="hljs-number">0</span>) { chunk1 = str.<span class="hljs-title function_">substr</span>(i, k).<span class="hljs-title function_">split</span>(<span class="hljs-string">''</span>).<span class="hljs-title function_">reverse</span>().<span class="hljs-title function_">join</span>(<span class="hljs-string">''</span>) } <span class="hljs-keyword">else</span> { chunk1 = str.<span class="hljs-title function_">substr</span>(i, k) } chunkStr += chunk1 } <span class="hljs-keyword">return</span> chunkStr }

<span class="hljs-keyword">let</span> str1 = <span class="hljs-string">'abcdefghijkl'</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">strRevChunk</span>(str1, <span class="hljs-number">2</span>))</pre></div><p id="ae49">The code block also contains an example of how the function is used, where str1 = ‘abcdefghijkl’ and k = 2. Here we can see the function is called with str1 and 2 as its arguments, and the function will return the modified string “badcefghijkl”</p><div id="f328"><pre><span class="hljs-keyword">let</span> str1 = <span class="hljs-s

Options

tring">'abcdefghijkl'</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">strRevChunk</span>(str1, <span class="hljs-number">2</span>)) <span class="hljs-title class_">Output</span> - badcefghijkl</pre></div><p id="ae04">Happy learning 😄</p><p id="f3ad">Do support our publication by following it</p><div id="326f" class="link-block"> <a href="https://medium.com/thefreshwrites"> <div> <div> <h2>The Fresh Writes</h2> <div><h3>We support small publishers to enhance their articles and increase their growth</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Yqvp7nKT1C7ocQ9dzDewfA.png)"></div> </div> </div> </a> </div><div id="5bf1" class="link-block"> <a href="https://readmedium.com/introduction-to-spark-architecture-fadc9829d3f5"> <div> <div> <h2>Introduction to Spark Architecture</h2> <div><h3>It is an Open Source, Unified Analytics Engine for Large Scale Data Processing.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*EHbFLR_yKf7W62cgPAuNwA.jpeg)"></div> </div> </div> </a> </div></article></body>

Coding: Reverse Alternate K-length Chunks of a String In Javascript

In this article, we will be discussing a code snippet that demonstrates how to reverse alternate chunks of a string. The code defines a function called strRevChunk(str, k) that takes in two parameters: a string str and an integer k.

A “chunk” is defined as a substring of the original string, with a length of “k” characters. The loop starts at the first character of the string (index 0) and increments by “k” characters each time, so it will grab chunks of the string in the following order: 0-k, k-2k, 2k-3k, etc.

If the current index (i) of the loop is divisible by 2*k, the function takes the current chunk and reverses it using the .split(), .reverse(), and .join() methods. Otherwise, it simply assigns the current chunk to a variable “chunk1” without reversing it.

The reversed or non-reversed chunk is then added to the “chunkStr” variable, which is initially an empty string. Once the loop completes, the “chunkStr” variable will contain the modified version of the original string, with chunks reversed every other iteration. The function then returns the modified “chunkStr” variable.

function strRevChunk(str, k) {
  let strLen = str.length
  let chunk1, chunk2
  let chunkStr = ""
  for (let i = 0; i < strLen; i += k) {
    if (i % (2*k) == 0) {
      chunk1 = str.substr(i, k).split('').reverse().join('')
    } else {
      chunk1 = str.substr(i, k)
    }
    chunkStr += chunk1
  }
  return chunkStr
}

let str1 = 'abcdefghijkl'
console.log(strRevChunk(str1, 2))

The code block also contains an example of how the function is used, where str1 = ‘abcdefghijkl’ and k = 2. Here we can see the function is called with str1 and 2 as its arguments, and the function will return the modified string “badcefghijkl”

let str1 = 'abcdefghijkl'
console.log(strRevChunk(str1, 2))
Output - badcefghijkl

Happy learning 😄

Do support our publication by following it

JavaScript
Code
Coding
Data Structures
Recommended from ReadMedium