Hướng dẫn javascript reverse string - chuỗi đảo ngược javascript

Bài viết này dựa trên tập lệnh thuật toán cơ bản của Freecodecamp

Nội dung chính

  • 1. Đảo ngược một chuỗi với các chức năng tích hợp
  • 2. Đảo ngược một chuỗi với một vòng lặp giảm cho vòng lặp
  • 3. Đảo ngược một chuỗi với đệ quy
  • Tài nguyên

Đảo ngược một chuỗi là một trong những câu hỏi JavaScript thường gặp nhất trong vòng phỏng vấn kỹ thuật. Người phỏng vấn có thể yêu cầu bạn viết các cách khác nhau để đảo ngược chuỗi hoặc họ có thể yêu cầu bạn đảo ngược một chuỗi mà không cần sử dụng các phương thức được xây dựng hoặc họ thậm chí có thể yêu cầu bạn đảo ngược một chuỗi bằng cách sử dụng đệ quy. is one of the most frequently asked JavaScript question in the technical round of interview. Interviewers may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or they may even ask you to reverse a string using recursion. is one of the most frequently asked JavaScript question in the technical round of interview. Interviewers may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or they may even ask you to reverse a string using recursion.

Có khả năng hàng chục cách khác nhau để thực hiện nó, không bao gồm chức năng đảo ngược tích hợp, vì JavaScript không có.reverse function, as JavaScript does not have one.reverse function, as JavaScript does not have one.

Dưới đây là ba cách thú vị nhất của tôi để giải quyết vấn đề đảo ngược một chuỗi trong JavaScript.

Thử thách thuật toán

Đảo ngược chuỗi được cung cấp. Bạn có thể cần phải biến chuỗi thành một mảng trước khi bạn có thể đảo ngược nó. Kết quả của bạn phải là một chuỗi.You may need to turn the string into an array before you can reverse it.Your result must be a string.
You may need to turn the string into an array before you can reverse it.
Your result must be a string.
function reverseString[str] {
    return str;
}
reverseString["hello"];

Cung cấp các trường hợp thử nghiệm

  • đảo ngược ["Xin chào"] sẽ trở thành "Olleh" should become “olleh” should become “olleh”
  • Reversestring [Hồi Howdy,] sẽ trở thành should become “ydwoH” should become “ydwoH”
  • Reversestring [Lời chào từ Trái đất] sẽ trả lại cho Htrae morf sgniteerg, should return”htraE morf sgniteerG” should return”htraE morf sgniteerG”

1. Đảo ngược một chuỗi với các chức năng tích hợp

2. Đảo ngược một chuỗi với một vòng lặp giảm cho vòng lặp

  • 3. Đảo ngược một chuỗi với đệ quy
  • Tài nguyên
  • Đảo ngược một chuỗi là một trong những câu hỏi JavaScript thường gặp nhất trong vòng phỏng vấn kỹ thuật. Người phỏng vấn có thể yêu cầu bạn viết các cách khác nhau để đảo ngược chuỗi hoặc họ có thể yêu cầu bạn đảo ngược một chuỗi mà không cần sử dụng các phương thức được xây dựng hoặc họ thậm chí có thể yêu cầu bạn đảo ngược một chuỗi bằng cách sử dụng đệ quy. is one of the most frequently asked JavaScript question in the technical round of interview. Interviewers may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or they may even ask you to reverse a string using recursion.
function reverseString[str] {
    // Step 1. Use the split[] method to return a new array
    var splitString = str.split[""]; // var splitString = "hello".split[""];
    // ["h", "e", "l", "l", "o"]
 
    // Step 2. Use the reverse[] method to reverse the new created array
    var reverseArray = splitString.reverse[]; // var reverseArray = ["h", "e", "l", "l", "o"].reverse[];
    // ["o", "l", "l", "e", "h"]
 
    // Step 3. Use the join[] method to join all elements of the array into a string
    var joinArray = reverseArray.join[""]; // var joinArray = ["o", "l", "l", "e", "h"].join[""];
    // "olleh"
    
    //Step 4. Return the reversed string
    return joinArray; // "olleh"
}
 
reverseString["hello"];

Có khả năng hàng chục cách khác nhau để thực hiện nó, không bao gồm chức năng đảo ngược tích hợp, vì JavaScript không có.reverse function, as JavaScript does not have one.

function reverseString[str] {
    return str.split[""].reverse[].join[""];
}
reverseString["hello"];

2. Đảo ngược một chuỗi với một vòng lặp giảm cho vòng lặp

function reverseString[str] {
    // Step 1. Create an empty string that will host the new created string
    var newString = "";
 
    // Step 2. Create the FOR loop
    /* The starting point of the loop will be [str.length - 1] which corresponds to the 
       last character of the string, "o"
       As long as i is greater than or equals 0, the loop will go on
       We decrement i after each iteration */
    for [var i = str.length - 1; i >= 0; i--] { 
        newString += str[i]; // or newString = newString + str[i];
    }
    /* Here hello's length equals 5
        For each iteration: i = str.length - 1 and newString = newString + str[i]
        First iteration:    i = 5 - 1 = 4,         newString = "" + "o" = "o"
        Second iteration:   i = 4 - 1 = 3,         newString = "o" + "l" = "ol"
        Third iteration:    i = 3 - 1 = 2,         newString = "ol" + "l" = "oll"
        Fourth iteration:   i = 2 - 1 = 1,         newString = "oll" + "e" = "olle"
        Fifth iteration:    i = 1 - 1 = 0,         newString = "olle" + "h" = "olleh"
    End of the FOR Loop*/
 
    // Step 3. Return the reversed string
    return newString; // "olleh"
}
 
reverseString['hello'];
function reverseString[str] {
    var newString = "";
    for [var i = str.length - 1; i >= 0; i--] {
        newString += str[i];
    }
    return newString;
}
reverseString['hello'];

3. Đảo ngược một chuỗi với đệ quy

Tài nguyên

  • Đảo ngược một chuỗi là một trong những câu hỏi JavaScript thường gặp nhất trong vòng phỏng vấn kỹ thuật. Người phỏng vấn có thể yêu cầu bạn viết các cách khác nhau để đảo ngược chuỗi hoặc họ có thể yêu cầu bạn đảo ngược một chuỗi mà không cần sử dụng các phương thức được xây dựng hoặc họ thậm chí có thể yêu cầu bạn đảo ngược một chuỗi bằng cách sử dụng đệ quy. is one of the most frequently asked JavaScript question in the technical round of interview. Interviewers may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or they may even ask you to reverse a string using recursion.
"hello".substr[1]; // "ello"
  • Có khả năng hàng chục cách khác nhau để thực hiện nó, không bao gồm chức năng đảo ngược tích hợp, vì JavaScript không có.reverse function, as JavaScript does not have one.
"hello".charAt[0]; // "h"

Dưới đây là ba cách thú vị nhất của tôi để giải quyết vấn đề đảo ngược một chuỗi trong JavaScript.

function reverseString[str] {
  if [str === ""] // This is the terminal case that will end the recursion
    return "";
  
  else
    return reverseString[str.substr[1]] + str.charAt[0];
/* 
First Part of the recursion method
You need to remember that you won’t have just one call, you’ll have several nested calls

Each call: str === "?"        	                  reverseString[str.subst[1]]     + str.charAt[0]
1st call – reverseString["Hello"]   will return   reverseString["ello"]           + "h"
2nd call – reverseString["ello"]    will return   reverseString["llo"]            + "e"
3rd call – reverseString["llo"]     will return   reverseString["lo"]             + "l"
4th call – reverseString["lo"]      will return   reverseString["o"]              + "l"
5th call – reverseString["o"]       will return   reverseString[""]               + "o"

Second part of the recursion method
The method hits the if condition and the most highly nested call returns immediately

5th call will return reverseString[""] + "o" = "o"
4th call will return reverseString["o"] + "l" = "o" + "l"
3rd call will return reverseString["lo"] + "l" = "o" + "l" + "l"
2nd call will return reverserString["llo"] + "e" = "o" + "l" + "l" + "e"
1st call will return reverserString["ello"] + "h" = "o" + "l" + "l" + "e" + "h" 
*/
}
reverseString["hello"];
function reverseString[str] {
  if [str === ""]
    return "";
  else
    return reverseString[str.substr[1]] + str.charAt[0];
}
reverseString["hello"];

Thử thách thuật toán

function reverseString[str] {
  return [str === ''] ? '' : reverseString[str.substr[1]] + str.charAt[0];
}
reverseString["hello"];

Đảo ngược chuỗi được cung cấp. Bạn có thể cần phải biến chuỗi thành một mảng trước khi bạn có thể đảo ngược nó. Kết quả của bạn phải là một chuỗi.You may need to turn the string into an array before you can reverse it.Your result must be a string. is a small and simple algorithm that can be asked on a technical phone screening or a technical interview. You could take the short route in solving this problem, or take the approach by solving it with recursion or even more complex solutions.

Cung cấp các trường hợp thử nghiệm

đảo ngược ["Xin chào"] sẽ trở thành "Olleh" should become “olleh”
In this article, I’ll explain how to solve freeCodeCamp’s “Repeat a string repeat a string” challenge. This involves…

Reversestring [Hồi Howdy,] sẽ trở thành should become “ydwoH”
In this article, I’ll explain how to solve freeCodeCamp’s “Confirm the Ending” challenge.

Reversestring [Lời chào từ Trái đất] sẽ trả lại cho Htrae morf sgniteerg, should return”htraE morf sgniteerG”
This article is based on Free Code Camp Basic Algorithm Scripting “Factorialize a Number”

Hai cách để kiểm tra palindromes trong bài viết của JavaScriptthis dựa trên thuật toán cơ bản của Code Camp miễn phí Kiểm tra palindromes.This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.
This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.

Ba cách để tìm từ dài nhất trong một chuỗi trong bài viết của JavaScriptthis dựa trên tập lệnh thuật toán cơ bản Camp Camp miễn phí Tìm từ dài nhất trong một chuỗi.This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.
This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.

Ba cách để tiêu đề trường hợp một câu trong bài viết của JavaScriptthis dựa trên thuật toán cơ bản Camp Camp miễn phí Trường hợp tiêu đề của một câu.This article is based on Free Code Camp Basic Algorithm Scripting “Title Case a Sentence”.
This article is based on Free Code Camp Basic Algorithm Scripting “Title Case a Sentence”.

Nếu bạn có giải pháp riêng hoặc bất kỳ đề xuất nào, hãy chia sẻ chúng dưới đây trong các ý kiến.

Hoặc bạn có thể theo dõi tôi trên Medium, Twitter, GitHub và LinkedIn, ngay sau khi bạn nhấp vào trái tim xanh bên dưới ;-]Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-]Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-]

#Ở lại, #Keeponhacking & #MakeithAppen!

Tài nguyên

  • Phương thức chia [] - MDN
  • Phương thức đảo ngược [] - MDN
  • tham gia [] Phương thức - MDN
  • Chuỗi.length - mdn
  • Phương thức Subr [] - MDN
  • Phương pháp Charat [] - MDN

Học mã miễn phí. Chương trình giảng dạy nguồn mở của Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Bài Viết Liên Quan

Chủ Đề