DEV Community

loading...

Challenge Algo #6 - Alphabetic Shift

farid212 profile image Farid ・1 min read

Given a string, replace each its character by the next one in the English alphabet (z woule be replaced by a).

Example

For inputString = "crazy", the output should be alphabeticShift(String) = "dsbaz".

const alphabeticShift = inputString =>{
    const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

    let inputShifted = inputString.split('');

    for (let i = 0; i < inputShifted.length; i++) {
        let indexValue = 0;

        if(inputShifted[i] !== 'z'){
            indexValue = alphabet.indexOf(inputShifted[i])+1;
        }
        inputShifted[i] = alphabet[indexValue];
    }
    return inputShifted.join('');
}

console.log(alphabeticShift('crazy')); //dsbaz
Enter fullscreen mode Exit fullscreen mode

as usually if you know a better way or more efficient way please share it in comment.

Thanks guys.

Discussion (0)

pic
Editor guide