-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay27.js
More file actions
34 lines (27 loc) · 1.16 KB
/
Day27.js
File metadata and controls
34 lines (27 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Coding Challenge: Repeat a String
// Write a function called repeatString that takes two parameters:
// str: A string that needs to be repeated.
// num: An integer representing the number of times the string should be repeated.
// The function should repeat the input string str the specified number of times num and return the resulting string.
// Example usage:
// const repeatString = function (str, num) {
// let repeat = "";
// const maxLength = str.length * num;
// for (let i = 0; i < num; i++) {
// if (repeat.length < maxLength) {
// repeat += str;
// } else {
// break; // Exit loop if the maximum length is reached
// }
// }
// return repeat;
// }
// console.log(repeatString("abc", 5)); // Output: "abcabcabcabcabc"
// 2nd method
const repeatString = (str,num) =>{
return num > 0 ? str.repeat(num): str; // :str mean if num =0 then simpley return the str
}
console.log(repeatString("abc", 0)); // Output: "abcabcabcabcabc"
// Constraints:
// The input string str will contain only alphanumeric characters and punctuation marks.
// The input number num will be a non-negative integer.