JavaScript 中 n 次运球字符串
我们需要编写一个 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