Repeating letter string - JavaScript
我们需要编写一个 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++){
// using the String.prototype.repeat() function
res += str[i].repeat(n);
};
return res;
};
console.log(repeatNTimes(str, 2));输出
以下是控制台中的输出 -
hhooww aarree yyoouu
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP