How to move the first character in a string array to the front?

I cant seem to find a solution on how to move the first character from a string of an array to the front. ex:

let str = [“Pancakes”]
str.anArrayMethod(move me to, here)
console.log(str) ===> [“ancakesP”]

I would think that .slice() or .splice() would solves this, but .slice() removes the first character completely and .splice() will just get rid of the entire string.

This seems like a pretty easy and basic solution, but I cant figure it out.

i read something about using .substring(), but that was from a 2012 thread and is not a working method option in VS Code.

You can use slice and just add the sliced letter at the end

Hint: I was able to accomplish this using three methods:
• split
• slice
• join

There’s no special thing called a “string array”.

You have an array. That array has one element, at index 0. That element is a string.

If you want to change that string, you need to get the element at index 0, do something string related to it. That will create a new string, so you set that new string as the element at index 0.


If you apply “slice” or “splice” to the array, then it will, naturally, apply the operation to the array, not the string that is the first element in the array.

Once you have the string instead, there are a number of methods you can use (you could do what @xynoan did or you could loop over the string and create a new one, or you could use replace on the string, etc)

1 Like

ok so that was what I was sort of doing originally. my question is related to a code wars problem and this is how far i got last week. i just havent used join yet becuase it would join the entire array. (im disregarding the punctuation request for the time being).

type or paste code her// INSTRUCTIONS: Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

// EXAMPLE OUTPUT: pigIt('Pig latin is cool'); ===> igPay atinlay siay oolcay
// EXAMPLE OUTPUT: pigIt('Hello world !'); =======> elloHay orldway !

// ATTEMPT 1:
function pigIt(str){
    // separate str into array => [Hello, world]
    let strSplitted = str.split(' ')
    let strFirstLetters = [];
    let strRearranged = [];
    let strReassembled = []
    console.log(strSplitted); 
    for (let i=0; i<strSplitted.length; i++) {
        // loop through and take first character of each word 
        strFirstLetters.push(strSplitted[i][0])
        strRearranged.push(strSplitted[i].slice(1))
        // and add it to front => [elloH, orldw]
        // then add 'ay' at end => [elloHay, orldway]
        strReassembled = strRearranged.push(strFirstLetters[i] + "ay")
        // then return as a full string
    }
    console.log(strFirstLetters);
    console.log(strRearranged);
    console.log(strReassembled);
}

console.log(pigIt("Hello world"));

this was what i was working on last week. my question is related to a code wars problem and i am using a loop along with the split and slice.

// INSTRUCTIONS: Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

// EXAMPLE OUTPUT: pigIt('Pig latin is cool'); ===> igPay atinlay siay oolcay
// EXAMPLE OUTPUT: pigIt('Hello world !'); =======> elloHay orldway !

// ATTEMPT 1:
function pigIt(str){
    // separate str into array => [Hello, world]
    let strSplitted = str.split(' ')
    let strFirstLetters = [];
    let strRearranged = [];
    let strReassembled = []
    console.log(strSplitted); 
    for (let i=0; i<strSplitted.length; i++) {
        // loop through and take first character of each word 
        strFirstLetters.push(strSplitted[i][0])
        strRearranged.push(strSplitted[i].slice(1))
        // and add it to front => [elloH, orldw]
        // then add 'ay' at end => [elloHay, orldway]
        strReassembled = strRearranged.push(strFirstLetters[i] + "ay")
        // then return as a full string
    }
    console.log(strFirstLetters);
    console.log(strRearranged);
    console.log(strReassembled);
}

console.log(pigIt("Hello world"));

What do you have going on here?

Personally when im making a new string i like to use an empty string and += to add words or letters to it, i.e.:

let pigLatin = '';
pigLatin += 'ello' + 'H' + 'ay ';
pigLatin += 'orld' + 'W' + 'ay';
console.log(pigLatin)//elloHay orldWay

You can get this to work with all these arrays storing strings, but you will need to fix that line of code. And do you even need to keep the strings stored in a array? Or could you get them in the loop and rearrange the letters and just add them to a string?