Repeat a string repeat a string-freecodecamp算法题目
Repeat a string repeat a string(重复输出字符串)
- 要求
- 重复一个指定的字符串
num次 - 如果
num是一个负数则返回一个空字符串。
- 重复一个指定的字符串
- 思路
- 将给定的字符串赋给定义的变量temp
- 利用判断语句,如果重复次数不大于0;返回空字符串‘’,如果重复次数大于0,进入for循环
- 在for循环中利用.concat()将字符串重复连接
- 代码
function repeat(str, num) {
// 请把你的代码写在这里
var temp = str;
if(num>0){
for(var i = 1;i<num;i++){
str = str.concat(temp);
}
return str;}
else {
return '';
}
} repeat("abc", 3);
- 相关链接
- https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String
- http://www.runoob.com/jsref/jsref-concat-string.html
Repeat a string repeat a string-freecodecamp算法题目的更多相关文章
- Map the Debris -freecodecamp算法题目
Map the Debris 1.要求 返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: ...
- Spinal Tap Case -freecodecamp算法题目
Spinal Tap Case 1.要求 将字符串转换为 spinal case. Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也 ...
- Search and Replace -freecodecamp算法题目
Search and Replace 1.要求 使用给定的参数对句子执行一次查找和替换,然后返回新句子. 第一个参数是将要对其执行查找和替换的句子. 第二个参数是将被替换掉的单词(替换前的单词). 第 ...
- FreeCodecamp:Repeat a string repeat a string
要求: 重要的事情说3遍! 重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. 结果: repeat("*", 3) 应该返回"***". ...
- JavaScript string repeat methods All In One
JavaScript string repeat methods All In One There are many ways in the ES-Next ways repeat ES2015 / ...
- 程序兵法:Java String 源码的排序算法(一)
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第103篇原创 <程序兵法:Java Str ...
- 17-比赛1 B - 子串计算 Chef and his string challenge (string的运用)
Chef's best friend Jerry gives Chef a string A and wants to know the number of string A that can be ...
- Don’t Repeat Yourself,Repeat Yourself
Don't Repeat Yourself,Repeat Yourself Don't repeat yourself (DRY, or sometimes do not repeat yoursel ...
- [转] 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧
这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多已经吐槽无力,但这次实在忍不住了就又爆发了一把.写得太长干脆单独开了一帖. 顺带广告:对JVM感兴趣的同学们同志们 ...
随机推荐
- IT兄弟连 JavaWeb教程 监听器3
监听域对象中属性变更的监听器 域对象中属性的变更的事件监听器就是用来监听ServletContext.HttpSession.HttpServletRequest这三个对象中的属性变更信息事件的监听器 ...
- SpringBoot2.0 基础案例(06):引入JdbcTemplate,和多数据源配置
一.JdbcTemplate对象 1.JdbcTemplate简介 在Spring Boot2.0框架下配置数据源和通过JdbcTemplate访问数据库的案例. SpringBoot对数据库的操作在 ...
- Composer的简单使用
.前期准备: 1.下载安装包,https://getcomposer.org/download/ 2.在php.ini文档中打开extension=php_openssl.dll 3.下载php_ss ...
- 《SQL 进阶教程》case :用一条 SQL 语句进行不同条件的统计
进行不同条件的统计是case表达式的著名用法之一 select name,sum(case when sex = 1 then population else 0 end) as cnt_m,sum( ...
- 6.计算字段 ---SQL
提示:客户端与服务器的格式在SQL语句内可完成的许多转换和格式化工作都可以直接在客户端应用程序内完成.但一般来说,在数据库服务器上完成这些操作比在客户端中完成要快得多. 一.拼接字段 拼接(conca ...
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- 线程池(2)Executors.newFixedThreadPool
例子: ExecutorService es = Executors.newFixedThreadPool(5); try { for (int i = 0; i < 20; i++) { Ru ...
- HDU 1027 G - Can you answer these queries?
http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Time Limit: 4000/2000 M ...
- java学习笔记(2)——数组
1.创建数组: int[] a = new int[n];//数组长度n不要求为常数,一旦创建了数组,其大小不可改变 int[] a = {0,1,2,3};//也可这样定义 获得数组元素的个数:ar ...
- 第一天课程-html基础
一.课程内容: 1.安装需要的软件 安装了三个软件:Adobe Dreamweaver,EmEditor,FSCapture.分别是前端开发软件.功能强大的文本编辑器,截图录屏软件 2.了解文件格式. ...