1

I have string like below:

ABCDEFGH

And i want to reverse it like below:

GHEFCDAB

i have tried this:

  var str = "ABCDEFGH";
  var splitString = str.split("");
  var reverseArray = splitString.reverse();
  var joinArray = reverseArray.join(""); 

And return like below:

HGFEDCBA

I Know it wrong, and it just reverse it one way. how to reverse string in javascript with that condition? Many thank before.

4
  • So, you want to reverse every couple of letters, but leave the two letters in each group in the same order? Commented Jan 29, 2020 at 11:17
  • 1
    Does this answer your question? How do you reverse a string in place in JavaScript? Commented Jan 29, 2020 at 11:18
  • @OscarPaz yes, thats right. can you help me please? Commented Jan 29, 2020 at 11:18
  • @PrestonGarvey wait, im still reading it. Commented Jan 29, 2020 at 11:19

4 Answers 4

4

You can do this, using regex : let splited = str.match(/.{1,2}/g).reverse().join('');

str.match(/.{1,2}/g) to transform ABCDEFGH to [AB, CD, EF, GH]
reverse() to transform [AB, CD, EF, GH] to [GH, EF, CD, AB]
join('') to transform [GH, EF, CD, AB] to GHEFCDAB

You can take a look here

Sign up to request clarification or add additional context in comments.

Comments

1

Pairing string reverse

Using empty String

const str = 'ABCDEFGH'
var emptyStr = '';

for(let i = str.length; i>1; i--){
  emptyStr += str.slice(i-2,i)
  i--
}
console.log(emptyStr);

using empty Array

const str = 'ABCDEFGH'
const rev = str.split('');
const revStr = []

for(let i = str.length; i >=1; i--){
const strPair = rev.slice(i-2,i).join('');
  i--;
  revStr.push(strPair)
  }
console.log(revStr.join(''))

Output: GHEFCDAB

Comments

0
class RevStr{
 
    constructor(input){
        this.str =input;
    }

    reverseStr() {
        let str = this.str.match(/.{1,2}/g)
        let end = str.length-1;
        let start = 0;

        while (start < end) {
            let temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++;
            end--;
        }
        console.log(str.join(''));
    }

}
let s = new RevStr("abcdefgh");
s.reverseStr();

Comments

0

Here are 3 simple solutions to reverse a string in JavaScript:

// Solution-1: Using reverse() method
function reverseString1(str) {
    return str.split('').reverse().join('')
}

// Solution-2: Using for loop
function reverseString2(str) {
    let reversed = ''; 

    for(let character of str) {
        reversed = character + reversed; 
    }

    return reversed;
}

// Solution-3: Using reduce() method
function reverseString3(str) {
    return str.split('').reduce((reversed, character) => character + reversed, '')
}

console.log("Solution 1: ", reverseString1("happy")); 
console.log("Solution 2: ", reverseString2("database")); 
console.log("Solution 3: ", reverseString3("loop")); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.