Hướng dẫn how do you repeat a string in javascript? - làm thế nào để bạn lặp lại một chuỗi trong javascript?

Hướng dẫn how do you repeat a string in javascript? - làm thế nào để bạn lặp lại một chuỗi trong javascript?

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:

  1. Sử dụng vòng lặp trong thời gian
  2. Sử dụng đệ quy
  3. Sử dụng phương thức ES6 REBARE ()

Mô tả thử thách thuật toán

Lặp lại một chuỗi đã cho (đối số thứ nhất)
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 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.
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

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:

while (condition)
  statement

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:


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ư
    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.base case: this is a statement, usually within a conditional clause like
    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, that stops the recursion.
  • 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) return "";
    Second part of the recursion method
      4th call will return      ""
      3rd call will return     "abc"
      2nd call will return     "abc"
      1st call will return     "abc"
    The final call is a concatenation of all the strings
    return "abc" + "abc" + "abc"; // return "abcabcabc";
  */
}
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 "";
  if(times === 1) 
    return string;
  else 
    return string + repeatStringNumTimes(string, times - 1);
}
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ư
    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.

Đây là giải pháp:


function repeatStringNumTimes(string, times) {
  //Step 1. If times is positive, return the repeated string
  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

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 "".
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.
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.

Làm thế nào để bạn lặp lại một chuỗi mã?

Cách tiếp cận số 3: Lặp lại một chuỗi bằng phương thức ES6 lặp lại () cho giải pháp này, bạn sẽ sử dụng chuỗi. nguyên mẫu. Phương thức lặp lại (): Phương thức lặp lại () tạo cấu trúc 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, được nối với nhau.using ES6 repeat() method For this solution, you'll use the String. prototype. repeat() method: The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

Làm thế nào để bạn lặp lại một chuỗi nhiều lần?

Phương thức lặp lại () được sử dụng để trả về chuỗi có giá trị là sự kết hợp của thời gian đếm chuỗi đã cho trước.Nếu chuỗi trống hoặc số đếm bằng 0 thì chuỗi trống được trả về.Cú pháp: Chuỗi. is used to return String whose value is the concatenation of given String repeated count times. If the string is empty or the count is zero then the empty string is returned. Syntax: string.

Làm thế nào để bạn lặp lại văn bản trong html?

Sử dụng phần tử thay thế.Không có thẻ trong HTML, trừ khi bạn sử dụng JavaScript để tạo một yếu tố như vậy để chọn trong CSS. instead. There's no tag in HTML, unless you use JavaScript to create such an element in order to select that in CSS.

Làm thế nào để bạn lặp lại nút js?

Để gọi một hàm nhiều lần, bạn có thể sử dụng một vòng lặp ...
for (var i = 0; i <5; i ++) {bảng điều khiển.log ("hi");} Chạy mã mã.Mở rộng đoạn trích.....
for (var i = 0; i <5; i ++) bảng điều khiển.log ("hi");Chạy mã mã.....
.Chạy mã mã ..