Hướng dẫn reverse sentence in javascript - đảo ngược câu trong javascript

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

Nội dung chính ShowShow

  • Ví dụ 1: Đảo ngược một chuỗi bằng cách sử dụng cho vòng lặp
  • 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
  • Làm thế nào để bạn đảo ngược văn bản trong javascript?
  • Làm thế nào để bạn đảo ngược các từ trong một câu?
  • Làm thế nào để bạn đảo ngược một câu trong HTML?
  • Làm thế nào để bạn đảo ngược một câu lệnh chuỗi?

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

Enter a string: hello world
dlrow olleh
4.

Bên trong hàm

Enter a string: hello world
dlrow olleh
4,
  • Một biến báo chí trống được tạo ra.
  • Vòng
    Enter a string: hello world
    dlrow olleh
    6 đượ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
    7 đư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.
    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.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

Enter a string: hello world
dlrow olleh
4.
  • Bên trong hàm
  • Một biến báo chí trống được tạo ra.
  • Vòng
    Enter a string: hello world
    dlrow olleh
    6 đượ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
    7 đư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.

Enter a string: hello world
dlrow olleh
2

Bên trong hàm

Enter a string: hello world
dlrow olleh
4,

Enter a string: hello world
dlrow olleh
3

Vòng

Enter a string: hello world
dlrow olleh
6 đượ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
7 đư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.

Tôi đang tạo một chương trình JavaScript trong đó nếu người dùng nhập một sting như

Hướng dẫn reverse sentence in javascript - đảo ngược câu trong javascript

Đầu ra sẽ tạo ra:Dec 4, 2015 at 8:14

5

Tôi đã thử sử dụng chức năng

// 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 nhưng không nhận được đầu ra cần thiết.
Enter a string: hello world
dlrow olleh
5

Hỏi ngày 4 tháng 12 năm 2015 lúc 8:14Dec 4, 2015 at 8:14

Hãy thử sử dụng

// 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
Enter a string: hello world
dlrow olleh
8

Loại bỏ Dec 4, 2015 at 8:17

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);
7 tại
// 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 không phải là một chức năng; Thêm
Enter a string: hello
olleh
0 bên trong vòng lặp
Enter a string: hello world
dlrow olleh
6 để bao gồm thẻ
Enter a string: hello
olleh
2 trong đầu ra sau mỗi lần lặp
Enter a string: hello
olleh
3 để đặt đường phạm trong
Enter a string: hello
olleh
4 sau một từ được nhập vào tại
Enter a string: hello
olleh
5. Ví dụ: thử đầu vào của "Tôi yêu đất nước của tôi" tại
Enter a string: hello
olleh
6 tại Stacksnippets

Enter a string: hello world
dlrow olleh
9

________số 8

Enter a string: hello world
dlrow olleh
0

Loại bỏ Dec 4, 2015 at 8:17

gurvinder372gurvinder372gurvinder372gurvinder372

// 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 tại
// 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 không phải là một chức năng; Thêm
Enter a string: hello
olleh
0 bên trong vòng lặp
Enter a string: hello world
dlrow olleh
6 để bao gồm thẻ
Enter a string: hello
olleh
2 trong đầu ra sau mỗi lần lặp
Enter a string: hello
olleh
3 để đặt đường phạm trong
Enter a string: hello
olleh
4 sau một từ được nhập vào tại
Enter a string: hello
olleh
5. Ví dụ: thử đầu vào của "Tôi yêu đất nước của tôi" tại
Enter a string: hello
olleh
6 tại Stacksnippets9 gold badges70 silver badges91 bronze badges

2

Enter a string: hello world
dlrow olleh
1

________số 8

Đã trả lời ngày 4 tháng 12 năm 2015 lúc 8:17Dec 4, 2015 at 8:172 gold badges25 silver badges46 bronze badges

Hãy thử mã nàyDec 4, 2015 at 8:18

Vì bạn muốn in trong một dòng mớiRamanlfc

Hãy thử mã này9 gold badges70 silver badges91 bronze badges1 gold badge17 silver badges24 bronze badges

2

65,4K9 Huy hiệu vàng70 Huy hiệu bạc91 Huy hiệu Đồng2 gold badges25 silver badges46 bronze badges

OUTPUT:

Enter a string: hello world
dlrow olleh
3

vardDec 4, 2015 at 8:18

4.0172 Huy hiệu vàng25 Huy hiệu bạc46 Huy hiệu đồngRamanlfcDec 4, 2015 at 8:19

Đã trả lời ngày 4 tháng 12 năm 2015 lúc 8:181 gold badge17 silver badges24 bronze badgesJacob George

Ramanlfcramanlfc1 gold badge15 silver badges28 bronze badges

2

Làm thế nào để bạn đảo ngược văn bản trong javascript?

Làm thế nào để bạn đảo ngược các từ trong một câu? i.e. [ 's', 'k', 'e', 'e', 'G', ' ', 'r', 'o', 'f', ' ', 's', 'k', 'e', 'e', 'G' ] Use join() function in JavaScript to join the elements of an array into a string.

Làm thế nào để bạn đảo ngược các từ trong một câu?

Làm thế nào để bạn đảo ngược một câu trong HTML?

Làm thế nào để bạn đảo ngược một câu lệnh chuỗi?

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

Bên trong hàm

Một biến báo chí trống được tạo ra.

Vòng

Enter a string: hello world
dlrow olleh
6 đượ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
7 đư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.

Làm thế nào để bạn đảo ngược một câu trong HTML?

Làm thế nào để bạn đảo ngược một câu lệnh chuỗi? tag is used to override the current text direction.

Làm thế nào để bạn đảo ngược một câu lệnh chuỗi?

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 StringBuilder or StringBuffer class has an in-build method reverse() to reverse the characters in the string. This method replaces the sequence of the characters in reverse order. The reverse method is the static method that has the logic to reverse a string in Java.