Swap Array of words (Music Chords)

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

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

cifra.push(cifra.shift());

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

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

Here is the code:

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

:slight_smile: