हमें एक JavaScript फ़ंक्शन लिखना होगा जो एक स्ट्रिंग और एक संख्या, मान लीजिए 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++){
// String.prototype.repeat() फ़ंक्शन का उपयोग करके
res += str[i].repeat(n);
};
return res;
};
console.log(repeatNTimes(str, 2));
कंसोल में आउटपुट होगा:
hhooww aarree yyoouu