Hướng dẫn reverse a string in javascript using inbuilt function - đảo ngược một chuỗi trong javascript bằng chức năng sẵn có

Ví dụ 1: Đảo ngược một chuỗi bằng cách sử dụng cho vòng lặp

// program to reverse a string

function reverseString(str) {

    // empty string
    let newString = "";
    for (let i = str.length - 1; i >= 0; i--) {
        newString += str[i];
    }
    return newString;
}

// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);

Đầu ra

Enter a string: hello world
dlrow olleh

Trong chương trình trên, người dùng được nhắc nhập một chuỗi. Chuỗi đó được chuyển đến hàm reverseString().

Bên trong hàm reverseString(),

  • Một biến báo chí trống được tạo ra.
  • Vòng lặp
    Enter a string: hello world
    dlrow olleh
    1 được sử dụng để lặp lại trên các chuỗi. Trong lần lặp đầu tiên,
    Enter a string: hello world
    dlrow olleh
    2 đưa ra vị trí của phần tử cuối cùng. Yếu tố đó được thêm vào biến báo chí. Quá trình này tiếp tục cho tất cả các yếu tố chuỗi.
    This process continues for all the string elements.
  • Giá trị của tôi giảm trong mỗi lần lặp và tiếp tục cho đến khi nó trở thành 0.0.

Ví dụ 2: Đảo ngược chuỗi bằng cách sử dụng các phương thức tích hợp

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);

Đầu ra

Enter a string: hello
olleh

Trong chương trình trên, người dùng được nhắc nhập một chuỗi. Chuỗi đó được chuyển đến hàm reverseString().

  • Bên trong hàm reverseString(),
  • Một biến báo chí trống được tạo ra.
  • Vòng lặp
    Enter a string: hello world
    dlrow olleh
    1 được sử dụng để lặp lại trên các chuỗi. Trong lần lặp đầu tiên,
    Enter a string: hello world
    dlrow olleh
    2 đưa ra vị trí của phần tử cuối cùng. Yếu tố đó được thêm vào biến báo chí. Quá trình này tiếp tục cho tất cả các yếu tố chuỗi.

Phương pháp 3: & NBSP;

Sử dụng toán tử lây lan thay vì chức năng chia () để chuyển đổi chuỗi thành mảng các ký tự, tức là ['g', 'e', ​​'e', ​​'k', 's', '', 'f', 'o', ',' r ',' ',' g ',' e ',' e ',' k ',' s ']. Tìm hiểu thêm về toán tử lây lan ở đây

skeeG rof skeeG
3
skeeG rof skeeG
0
skeeG rof skeeG
1Examples:

Input: str = "Geeks for Geeks"
Output:  "skeeG rof skeeG"

Input: str = "Hello"
Output: "olleH"

Có nhiều phương pháp để đảo ngược một chuỗi trong JavaScript Một số trong số chúng được thảo luận dưới đây:

Phương pháp 1:

  • Kiểm tra chuỗi đầu vào mà chuỗi đã cho trống hoặc chỉ có một ký tự hoặc nó không thuộc loại chuỗi thì nó trả về chuỗi không hợp lệ.
  • Nếu điều kiện ở trên Sai, hãy tạo một mảng nơi chúng ta có thể lưu trữ kết quả. Ở đây Revarray [] là mảng mới.
  • Vòng lặp qua mảng từ cuối đến từ đầu và đẩy từng vật phẩm trong revarray mảng [].
  • Sử dụng hàm nối () Chức năng được xây dựng sẵn trong JavaScript để tham gia các phần tử của một mảng thành một chuỗi.

Example:  

JavaScript

Enter a string: hello world
dlrow olleh
9

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
0
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
1

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
2
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
3
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
4

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
5
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
6
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
7
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
8
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
9

Enter a string: hello
olleh
0
Enter a string: hello
olleh
1
Enter a string: hello
olleh
2
Enter a string: hello
olleh
3

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
2
Enter a string: hello
olleh
5

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
2
Enter a string: hello
olleh
7

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
2
Enter a string: hello
olleh
9

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
2
Enter a string: hello world
dlrow olleh
1
Input: str = "Geeks for Geeks"
Output:  "skeeG rof skeeG"

Input: str = "Hello"
Output: "olleH"
2

Enter a string: hello
olleh
0
Input: str = "Geeks for Geeks"
Output:  "skeeG rof skeeG"

Input: str = "Hello"
Output: "olleH"
4

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
2
Enter a string: hello
olleh
5

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
2
Enter a string: hello
olleh
1
Input: str = "Geeks for Geeks"
Output:  "skeeG rof skeeG"

Input: str = "Hello"
Output: "olleH"
9
skeeG rof skeeG
0
skeeG rof skeeG
1

Enter a string: hello
olleh
5

skeeG rof skeeG
3
Enter a string: hello world
dlrow olleh
1
skeeG rof skeeG
5

skeeG rof skeeG
6

Output:

skeeG rof skeeG

Phương pháp 2:

  • Sử dụng chức năng chia tách () ', ' ', 'Chuyên viên máy tính' ]
  • Sử dụng hàm lart () trong javascript để đảo ngược mảng các ký tự, tức là ['s', 'k', 'e', ​​'e', ​​'g', '', 'r', 'o', 'f', '', 's', 'k', 'e', ​​'e', ​​'g']]]
  • Sử dụng hàm tham gia () trong javascript để tham gia các phần tử của một mảng thành một chuỗi.

Example:  

JavaScript

Enter a string: hello world
dlrow olleh
9

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
0
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
1

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
5
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
6
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
7
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
8
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
9

Enter a string: hello
olleh
5

skeeG rof skeeG
3
Enter a string: hello world
dlrow olleh
1
skeeG rof skeeG
5

skeeG rof skeeG
6

Output:

skeeG rof skeeG

Phương pháp 2:

  • Sử dụng chức năng chia tách () ', ' ', 'Chuyên viên máy tính' ]
  • Sử dụng hàm lart () trong javascript để đảo ngược mảng các ký tự, tức là ['s', 'k', 'e', ​​'e', ​​'g', '', 'r', 'o', 'f', '', 's', 'k', 'e', ​​'e', ​​'g']]]
  • Sử dụng hàm tham gia () trong javascript để tham gia các phần tử của một mảng thành một chuỗi.

Example:  

JavaScript

Enter a string: hello world
dlrow olleh
9

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
0
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
1

skeeG rof skeeG
3
Enter a string: hello world
dlrow olleh
1
skeeG rof skeeG
5

skeeG rof skeeG
6

Output:

skeeG rof skeeG

Method#4:

  • Phương pháp 2:
  • Sử dụng chức năng chia tách () ', ' ', 'Chuyên viên máy tính' ]

Example: 

Sử dụng hàm lart () trong javascript để đảo ngược mảng các ký tự, tức là ['s', 'k', 'e', ​​'e', ​​'g', '', 'r', 'o', 'f', '', 's', 'k', 'e', ​​'e', ​​'g']]]

Enter a string: hello world
dlrow olleh
9

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
0
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
1

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
5
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
6
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
7
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
8
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
9

Enter a string: hello
olleh
5

skeeG rof skeeG
7
skeeG rof skeeG
8
skeeG rof skeeG
9

skeeG rof skeeG
6

Output:

skeeG rof skeeG