Hướng dẫn repeat a string javascript - lặp lại một chuỗi javascript

Phương thức 

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
1 sẽ lặp lại chuỗi string với số lần nhất định được cung cấp khi gọi phương thức. Phương thức sẽ trả về một chuỗi chính là n chuỗi string nối với nhau( n là số lần lặp được xác định khi gọi phương thức).

Hướng dẫn repeat a string javascript - lặp lại một chuỗi javascript

Hướng dẫn repeat a string javascript - lặp lại một chuỗi javascript

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: 

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
2
'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
2

Trong đó::

  • count là số lần lặp chuỗi string.

Cách sử dụng

Ví dụ: sử dụng phương thức 

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
1 để lặp một chuỗi.: sử dụng phương thức 
'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
1 để lặp một chuỗi.

Bài viết này được đăng tại [free tuts .net]

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <h2>Học lập trình miễn phí tại freetuts.net</h2>
 
        <button onclick="myFunction()">Run</button>
 
        <p id="demo"></p>
 
        <script>
            function myFunction() {
                var str = 'freetuts.net-'
                document.getElementById("demo").innerHTML = str.repeat(4);
            }
        </script>
    </body>
</html>

Kết quả

freetuts.net-freetuts.net-freetuts.net-freetuts.net-

Tham khảo: w3schools.com

The

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
4 method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.
'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
4
method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

Try it

Syntax

Parameters

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
5

An integer between

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
6 and
'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
7, indicating the number of times to repeat the string.

Return value

A new string containing the specified number of copies of the given string.

Exceptions

  • 'abc'.repeat(-1)    // RangeError
    'abc'.repeat(0)     // ''
    'abc'.repeat(1)     // 'abc'
    'abc'.repeat(2)     // 'abcabc'
    'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
    'abc'.repeat(1/0)   // RangeError
    
    ({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
    // 'abcabc' (repeat() is a generic method)
    
    8: repeat count must be non-negative.
  • 'abc'.repeat(-1)    // RangeError
    'abc'.repeat(0)     // ''
    'abc'.repeat(1)     // 'abc'
    'abc'.repeat(2)     // 'abcabc'
    'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
    'abc'.repeat(1/0)   // RangeError
    
    ({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
    // 'abcabc' (repeat() is a generic method)
    
    8: repeat count must be less than infinity and not overflow maximum string size.

Examples

Using repeat()

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)

Specifications

Specification
ECMAScript Language Specification # sec-string.prototype.repeat
# sec-string.prototype.repeat

Browser compatibility

BCD tables only load in the browser

See also

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.

Nội dung chính ShowShow

  • Mô tả thử thách thuật toán
  • Cung cấp các trường hợp thử nghiệm
  • 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
  • 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
  • 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
  • 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í
  • Làm thế nào để bạn lặp lại một chuỗi trong javascript?
  • Làm thế nào để bạn lặp lại một chuỗi nhiều lần?
  • Làm thế nào để bạn trả về một số lần một chuỗi?
  • Làm thế nào để bạn làm cho một cái gì đó lặp lại trong JavaScript?

JavaScript String R chế ra () phương thức REBEAR () trả về một chuỗi với một số bản sao của chuỗi.Phương thức lặp lại () trả về một chuỗi mới.Phương thức lặp lại () không thay đổi chuỗi gốc. The repeat() method returns a string with a number of copies of a string. The repeat() method returns a new string. The repeat() method does not change the original string.

  1. 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.
  2. Nội dung chính Show
  3. Mô tả thử thách thuật toán

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

Cung cấp các trường hợp thử nghiệm
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

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

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í

while (condition)
  statement

Có ba cách tiếp cận mà tôi bao gồm:

Sử dụng vòng lặp trong thời gian


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);

Sử dụng đệ quy

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

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í

  • Có ba cách tiếp cận mà tôi bao gồm: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.
  • Sử dụng vòng lặp trong thời gianrecursive case: this is the statement where the recursive function is called on itself.

Sử dụng đệ quy

freetuts.net-freetuts.net-freetuts.net-freetuts.net-
2

Sử dụng đệ quy

freetuts.net-freetuts.net-freetuts.net-freetuts.net-
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

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í

  • Đầ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:

freetuts.net-freetuts.net-freetuts.net-freetuts.net-
5

Và một lần nữa, không có bình luận:

freetuts.net-freetuts.net-freetuts.net-freetuts.net-
6

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:ternary operator as a shortcut for the if/else statement, like this:

freetuts.net-freetuts.net-freetuts.net-freetuts.net-
7

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.
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”
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”
This article is based on Free Code Camp Basic Algorithm Scripting “Factorialize a Number”

Phương thức

'abc'.repeat(-1)    // RangeError
'abc'.repeat(0)     // ''
'abc'.repeat(1)     // 'abc'
'abc'.repeat(2)     // 'abcabc'
'abc'.repeat(3.5)   // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0)   // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2)
// 'abcabc' (repeat() is a generic method)
04 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”.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”.
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”.
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 ;-)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 trong javascript?

Lặp lại () là một hàm sẵn có trong JavaScript được sử dụng để xây dựng một chuỗi mới chứa một số bản sao được chỉ định của chuỗi mà hàm này đã được gọi. Cú pháp: String.repeat (Đếm);string.repeat(count);string. repeat(count);

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

Chuỗi có thể được lặp lại n số lần và chúng ta có thể tạo một chuỗi mới có sự lặp lại.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ề.repeat() method 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.repeat() method 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.

Làm thế nào để bạn trả về một số lần một chuỗi?

Phương thức lặp lại () trả về một chuỗi đã được lặp lại một số lần mong muốn.Nếu tham số đếm không được cung cấp hoặc là giá trị của 0, phương thức lặp lại () sẽ trả về một chuỗi trống. returns a string that has been repeated a desired number of times. If the count parameter is not provided or is a value of 0, the repeat() method will return an empty string. returns a string that has been repeated a desired number of times. If the count parameter is not provided or is a value of 0, the repeat() method will return an empty string.

Làm thế nào để bạn làm cho một cái gì đó lặp lại trong JavaScript?

JavaScript String R chế ra () phương thức REBEAR () trả về một chuỗi với một số bản sao của chuỗi.Phương thức lặp lại () trả về một chuỗi mới.Phương thức lặp lại () không thay đổi chuỗi gốc. The repeat() method returns a string with a number of copies of a string. The repeat() method returns a new string. The repeat() method does not change the original string. The repeat() method returns a string with a number of copies of a string. The repeat() method returns a new string. The repeat() method does not change the original string.