Trong bài viết này, tôi sẽ giải thích cách giải quyết Freecodecamp, lặp lại một chuỗi lặp lại một thử thách chuỗi. Điều này liên quan đến việc lặp lại một chuỗi một số lần nhất định. Có ba cách tiếp cận mà tôi bao gồm: Một câu lệnh thực hiện câu lệnh của nó miễn là một điều kiện được chỉ định đánh giá là true. Một tuyên bố trong thời gian trông như thế này: với một điều kiện được đánh giá trước mỗi lần đi qua vòng lặp. Nếu điều kiện là đúng, câu lệnh được thực thi. Nếu điều kiện là sai, việc thực thi tiếp tục với bất kỳ câu lệnh nào sau khi vòng lặp. Câu lệnh được thực thi miễn là điều kiện là đúng. Đây là giải pháp:Mô tả thử thách thuật toán
Lặp lại một chuỗi đã cho [đối số thứ nhất]
1 lần [đối số thứ hai]. Trả về một chuỗi trống nếu repeatStringNumTimes["*", 3] should return "***".
repeatStringNumTimes["abc", 3] should return "abcabcabc".
repeatStringNumTimes["abc", 4] should return "abcabcabcabc".
repeatStringNumTimes["abc", 1] should return "abc".
repeatStringNumTimes["*", 8] should return "********".
repeatStringNumTimes["abc", -2] should return "".
1 không phải là một số dương.repeatStringNumTimes["*", 3] should return "***".
repeatStringNumTimes["abc", 3] should return "abcabcabc".
repeatStringNumTimes["abc", 4] should return "abcabcabcabc".
repeatStringNumTimes["abc", 1] should return "abc".
repeatStringNumTimes["*", 8] should return "********".
repeatStringNumTimes["abc", -2] should return "".
function repeatStringNumTimes[str, num] {
return str;
}
repeatStringNumTimes["abc", 3];
Cung cấp các trường hợp thử nghiệm
repeatStringNumTimes["*", 3] should return "***".
repeatStringNumTimes["abc", 3] should return "abcabcabc".
repeatStringNumTimes["abc", 4] should return "abcabcabcabc".
repeatStringNumTimes["abc", 1] should return "abc".
repeatStringNumTimes["*", 8] should return "********".
repeatStringNumTimes["abc", -2] should return "".
Cách tiếp cận số 1: Lặp lại một chuỗi với vòng lặp trong thời gian
while [condition]
statement
function repeatStringNumTimes[string, times] {
// Step 1. Create an empty string that will host the repeated string
var repeatedString = "";
// Step 2. Set the While loop with [times > 0] as the condition to check
while [times > 0] { // As long as times is greater than 0, the statement is executed
// The statement
repeatedString += string; // Same as repeatedString = repeatedString + string;
times--; // Same as times = times - 1;
}
/* While loop logic
Condition T/F repeatedString += string repeatedString times
First iteration [3 > 0] true "" + "abc" "abc" 2
Second iteration [2 > 0] true "abc" + "abc" "abcabc" 1
Third iteration [1 > 0] true "abcabc" + "abc" "abcabcabc" 0
Fourth iteration [0 > 0] false
}
*/
// Step 3. Return the repeated string
return repeatedString; // "abcabcabc"
}
repeatStringNumTimes["abc", 3];
Và một lần nữa, không có bình luận:
function repeatStringNumTimes[string, times] {
var repeatedString = "";
while [times > 0] {
repeatedString += string;
times--;
}
return repeatedString;
}
repeatStringNumTimes["abc", 3];
Cách tiếp cận #2: Lặp lại chuỗi bằng cách sử dụng một điều kiện và đệ quy
Recursion là một kỹ thuật để lặp lại một thao tác bằng cách có một chức năng gọi chính nó liên tục cho đến khi nó đến. Có một vài tính năng chính của đệ quy phải được bao gồm để nó hoạt động đúng.
- Đầu tiên là một trường hợp cơ sở: đây là một tuyên bố, thường là trong một mệnh đề có điều kiện như
3, ngăn chặn đệ quy.base case: this is a statement, usually within a conditional clause likerepeatStringNumTimes["*", 3] should return "***". repeatStringNumTimes["abc", 3] should return "abcabcabc". repeatStringNumTimes["abc", 4] should return "abcabcabcabc". repeatStringNumTimes["abc", 1] should return "abc". repeatStringNumTimes["*", 8] should return "********". repeatStringNumTimes["abc", -2] should return "".
3, that stops the recursion.repeatStringNumTimes["*", 3] should return "***". repeatStringNumTimes["abc", 3] should return "abcabcabc". repeatStringNumTimes["abc", 4] should return "abcabcabcabc". repeatStringNumTimes["abc", 1] should return "abc". repeatStringNumTimes["*", 8] should return "********". repeatStringNumTimes["abc", -2] should return "".
- Thứ hai là một trường hợp đệ quy: Đây là câu lệnh trong đó hàm đệ quy được tự gọi.recursive case: this is the statement where the recursive function is called on itself.
Đây là giải pháp:
function repeatStringNumTimes[string, times] {
// Step 1. Check if times is negative and return an empty string if true
if [times < 0] {
return "";
}
// Step 2. Check if times equals to 1 and return the string itself if it's the case.
if [times === 1] {
return string;
}
// Step 3. Use recursion
else {
return string + repeatStringNumTimes[string, times - 1]; // return "abcabcabc";
}
/*
First Part of the recursion method
You need to remember that you won’t have just one call, you’ll have several nested calls
times string + repeatStringNumTimes[string, times - 1]
1st call 3 "abc" + ["abc", 3 - 1]
2nd call 2 "abc" + ["abc", 2 - 1]
3rd call 1 "abc" => if [times === 1] return string;
4th call 0 "" => if [times 0] { // [3 > 0] => true
return string.repeat[times]; // return "abc".repeat[3]; => return "abcabcabc";
}
//Step 2. Else if times is negative, return an empty string if true
else {
return "";
}
}
repeatStringNumTimes["abc", 3];
Và một lần nữa, không có bình luận:
function repeatStringNumTimes[string, times] {
if [times > 0]
return string.repeat[times];
else
return "";
}
repeatStringNumTimes["abc", 3];
Cách tiếp cận #2: Lặp lại chuỗi bằng cách sử dụng một điều kiện và đệ quyternary operator as a shortcut for the if/else statement, like this:
times > 0 ? string.repeat[times] : "";
Recursion là một kỹ thuật để lặp lại một thao tác bằng cách có một chức năng gọi chính nó liên tục cho đến khi nó đến. Có một vài tính năng chính của đệ quy phải được bao gồm để nó hoạt động đúng.
repeatStringNumTimes["*", 3] should return "***".
repeatStringNumTimes["abc", 3] should return "abcabcabc".
repeatStringNumTimes["abc", 4] should return "abcabcabcabc".
repeatStringNumTimes["abc", 1] should return "abc".
repeatStringNumTimes["*", 8] should return "********".
repeatStringNumTimes["abc", -2] should return "".
0Đầu tiên là một trường hợp cơ sở: đây là một tuyên bố, thường là trong một mệnh đề có điều kiện như
repeatStringNumTimes["*", 3] should return "***".
repeatStringNumTimes["abc", 3] should return "abcabcabc".
repeatStringNumTimes["abc", 4] should return "abcabcabcabc".
repeatStringNumTimes["abc", 1] should return "abc".
repeatStringNumTimes["*", 8] should return "********".
repeatStringNumTimes["abc", -2] should return "".
3, ngăn chặn đệ quy.Thứ hai là một trường hợp đệ quy: Đây là câu lệnh trong đó hàm đệ quy được tự gọi.
Đây là giải pháp:
In this article, I’ll explain how to solve freeCodeCamp’s
“Confirm the Ending” challenge.
Cách tiếp cận #3: Lặp lại chuỗi bằng phương thức ES6 REBARE []
This article is based on Free Code Camp Basic Algorithm Scripting “Reverse a String”
Đối với giải pháp này, bạn sẽ sử dụng phương thức chuỗi.prototype.repeat []:
This article is based on Free Code Camp Basic Algorithm Scripting “Factorialize a Number”
Phương thức
4 xây dựng và trả về một chuỗi mới chứa số lượng bản sao được chỉ định của chuỗi mà nó được gọi là, được nối với nhau.repeatStringNumTimes["*", 3] should return "***".
repeatStringNumTimes["abc", 3] should return "abcabcabc".
repeatStringNumTimes["abc", 4] should return "abcabcabcabc".
repeatStringNumTimes["abc", 1] should return "abc".
repeatStringNumTimes["*", 8] should return "********".
repeatStringNumTimes["abc", -2] should return "".
This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.
Bạn có thể sử dụng toán tử ternary làm phím tắt cho câu lệnh if/other, như thế này:
This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.
Điều này có thể được đọc như:
This article is based on Free Code Camp Basic Algorithm Scripting “Title Case a Sentence”.
Sau đó, bạn có thể trả lại toán tử ternary trong chức năng của mình:
Tôi hy vọng bạn tìm thấy điều này hữu ích. Đây là một phần trong loạt bài viết của tôi Cách giải quyết các bài viết của FCC Thuật toán về các thách thức thuật toán Freecodecamp, trong đó tôi đề xuất một số giải pháp và giải thích từng bước những gì xảy ra dưới mui xe.Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-]
Hai cách để xác nhận kết thúc của một chuỗi trong JavaScriptin Bài viết này, tôi sẽ giải thích cách giải quyết FreeCodecamp, xác nhận thử thách kết thúc.
Ba cách để đảo ngược một chuỗi trong bài viết của JavaScriptthis dựa trên thuật toán cơ bản của Code Code miễn phí
- Ba cách để nhân viên hóa một số 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í
- 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.
- 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.
- 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.
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.