हमें एक 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++) {
res += str[i].repeat(n);
}
return res;
};
console.log(repeatNTimes(str, 2));
आउटपुट
hhooww aarree yyoouu