<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rogerdavid2</title>
    <description>The latest articles on DEV Community by rogerdavid2 (@rogerdavid24).</description>
    <link>https://dev.to/rogerdavid24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F668000%2F19883b5e-9999-415a-b748-43b2883fe287.png</url>
      <title>DEV Community: rogerdavid2</title>
      <link>https://dev.to/rogerdavid24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rogerdavid24"/>
    <language>en</language>
    <item>
      <title>Move first index value to the last index while maintaining original order on an array.</title>
      <dc:creator>rogerdavid2</dc:creator>
      <pubDate>Sun, 19 Dec 2021 06:23:09 +0000</pubDate>
      <link>https://dev.to/rogerdavid24/move-first-index-value-to-the-last-index-while-maintaining-original-order-on-an-array-2ne1</link>
      <guid>https://dev.to/rogerdavid24/move-first-index-value-to-the-last-index-while-maintaining-original-order-on-an-array-2ne1</guid>
      <description>&lt;p&gt;Assume we have an array &lt;code&gt;[5, 3, 7, 8]&lt;/code&gt; such that the first index value is moved to the back while preserving the original order resulting in &lt;code&gt;[3, 7, 8, 5]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's consider what we need to accomplish this. First, an array: &lt;code&gt;let values = [5, 3, 7, 8];&lt;/code&gt; we will be announcing &lt;code&gt;values&lt;/code&gt; as &lt;code&gt;list&lt;/code&gt; because &lt;code&gt;list&lt;/code&gt; is the named parameter for the function further along. &lt;/p&gt;

&lt;p&gt;With a for loop we will be traversing over an array to perform changes. &lt;/p&gt;

&lt;p&gt;A for loop can be broken into three parts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (initialization; test; update) {
      statements....
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, let's dissect our for loop. For our &lt;code&gt;initialization&lt;/code&gt; let's start at zero since arrays start with an index at zero. &lt;code&gt;let i = 0;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 0  1  2  3
[5, 3, 7, 8] // length is 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;test&lt;/code&gt; will evaluate when &lt;code&gt;i&lt;/code&gt; is less than the list’s length minus one. That is because our array &lt;code&gt;[5, 3, 7, 8]&lt;/code&gt; has a length of four, yet we only want to shift three values, but not including the number five. Yielding in &lt;code&gt;i &amp;lt; list.length - 1;&lt;/code&gt; and &lt;code&gt;update&lt;/code&gt; is represented by &lt;code&gt;i++&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; list.length - 1; i++) {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's think about our three values that are going to get shifted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 0  1  2  3
[5, 3, 7, 8] // Original Array; Length 4

 0  1  2  3
[3, 7, 8, 5] // Shifted Array; Length 4

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Shift   =  Original&lt;/code&gt;&lt;br&gt;
&lt;code&gt;list[0] = list[1];&lt;/code&gt;&lt;br&gt;
&lt;code&gt;list[1] = list[2];&lt;/code&gt;&lt;br&gt;
&lt;code&gt;list[3] = list[4];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Generally described as:&lt;br&gt;
&lt;code&gt;list[i] = list[i + 1];&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;list&lt;/code&gt; first index value will be placed in a variable &lt;code&gt;let idx = list[0];&lt;/code&gt; So &lt;code&gt;idx&lt;/code&gt; will be declared before the for loop since we are only shifting the remaining three values. &lt;/p&gt;

&lt;p&gt;After the for loop is done iterating we will type &lt;code&gt;list[list.length - 1] = idx;&lt;/code&gt; to store &lt;code&gt;idx&lt;/code&gt; into &lt;code&gt;list’s&lt;/code&gt; last index. After, we can wrap all the code in a function called &lt;code&gt;shiftLeft&lt;/code&gt; which will take an array as a parameter. &lt;/p&gt;

&lt;p&gt;Our final code is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";

let values = [5, 3, 7, 8];
console.log(values) // [5, 3, 7, 8];

function shiftLeft(list) {
    let idx = list[0];
    for (let i = 0; i &amp;lt; list.length - 1; i++) {
        list[i] = list[i + 1]
    }
    list[list.length - 1] = idx;
    console.log(list) 
}

shiftLeft(values); // [3, 7, 8, 5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
