# Swap Array of words (Music Chords)

**URL:** <https://forum.kirupa.com/t/swap-array-of-words-music-chords/656915>\
**Category:** programming\
**Created:** [February 13, 2023, 12:52pm UTC](https://forum.kirupa.com/t/swap-array-of-words-music-chords/656915 "2023-02-13T12:52:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cafony](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@Cafony](https://forum.kirupa.com/u/Cafony)\
**Post date:** [February 13, 2023, 12:52pm UTC](https://forum.kirupa.com/t/swap-array-of-words-music-chords/656915/1 "2023-02-13T12:52:40Z")

</div>

Im trying to make a music program to transpose chords.  
string[] cifra = { “C”, “D”, “E”, “F”, “G”, “A”, “B” };  
I think the funtion to swap one time is:  
for(int i=0;i\<cifra.Lenght-1;i++)  
{  
temp = cifra[i];  
cifra[i] = cifra[i+1];  
cifra[n + 1] = temp;  
}

The result should be  
{ “D”, “E”, “F”, “G”, “A”, “B”, “C” };

Thanks for help

---

<div class="post-metadata">

**Author:** ![PAEz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/paez/32/19607_2.png) [@PAEz](https://forum.kirupa.com/u/PAEz)\
**Post date:** [February 13, 2023, 2:36pm UTC](https://forum.kirupa.com/t/swap-array-of-words-music-chords/656915/2 "2023-02-13T14:36:34Z")

</div>

Are you trying to shift an array to the left? If so then this is all you need…

```javascript
cifra.push(cifra.shift());

```

`shift` takes the first element out of an array and `push` puts an element at the end of an array.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 13, 2023, 5:19pm UTC](https://forum.kirupa.com/t/swap-array-of-words-music-chords/656915/3 "2023-02-13T17:19:43Z")

</div>

Hi @Cafony - welcome to the forums! If you are looking for a more general swap mechanism, this tutorial should help you out:

> **[Swapping Items in an Array | kirupa.com](https://www.kirupa.com/html5/swapping_items_array_js.htm)**
>
> Did you know that there is no built-in way to swap two items in an array? In this article, you'll learn how to fix that!

Here is the code:

```auto
let myData = ["a", "b", "c", "d", "e", "f", "g"];

function swap(input, index_A, index_B) {
    let temp = input[index_A];

    input[index_A] = input[index_B];
    input[index_B] = temp;
}

swap(myData, 2, 5);
console.log(myData); // a b f d e c g

```

🙂
