Javascript - Nối một theo một số lần

Nối một theo một số lần

Viết một hàm JavaScript để nối một chuỗi đã cho n lần (mặc định là 1).

Ví dụ:

JavaScript: Concatenate a specific string for a specific number of times

Mã nguồn:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JavaScript program to convert the letters of a given string  in alphabetical order.</title>
</head>
<script>
	repeat = function repeat(str, count) {
    if(typeof(count) == "undefined") {
    count =1;
  }
  return count < 1 ? '' : new Array(count + 1).join(str);
    }
document.write(repeat('Ha!')+"<br/>");
document.write(repeat('Ha!',2)+"<br/>");
document.write(repeat('Ha!',3));
</script>
<body>

</body>
</html> 

Xem ví dụ

Lưu đồ thuật toán:

Flowchart: JavaScript- Concatenate a specific  string for a specific number of times


Chatbot Tư vấn Lộ trình CNTT 🤖
Chào bạn! Tôi có thể tư vấn về các lộ trình học CNTT dựa trên roadmap.w3typing.com. Hãy chọn một từ khóa dưới đây hoặc gõ câu hỏi của bạn (ví dụ: "Frontend", "Backend", "Python", "DevOps").