हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक स्ट्रिंग और एक संख्या लेता है, जैसे n, और फ़ंक्शन को एक नई स्ट्रिंग लौटानी चाहिए जिसमें मूल स्ट्रिंग के सभी अक्षर n बार दोहराए जाते हैं।
उदाहरण के लिए:यदि स्ट्रिंग है -
const str = 'how are you'
और संख्या n 2 है
तब आउटपुट होना चाहिए -
const output = 'hhooww aarree yyoouu'
उदाहरण
निम्नलिखित कोड है -
const str = 'how are you';
const repeatNTimes = (str, n) => {
let res = '';
for(let i = 0; i < str.length; i++){
// using the String.prototype.repeat() function
res += str[i].repeat(n);
};
return res;
};
console.log(repeatNTimes(str, 2)); आउटपुट
कंसोल में आउटपुट निम्नलिखित है -
hhooww aarree yyoouu