Skip to content

Commit 371dc02

Browse files
committed
repeat a string & confirm ending
1 parent c0eb008 commit 371dc02

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
1616
- [3. Factorialize a Number](#3-factorialize-a-number)
1717
- [4. Find the Longest Word in a String](#4-find-the-longest-word-in-a-string)
1818
- [5. Return Largest Numbers in Arrays Passed](#5-return-largest-numbers-in-arrays-passed)
19+
- [6. Confirm the Ending Passed](#6-confirm-the-ending-passed)
20+
- [7. Repeat a String Repeat a String](#7-repeat-a-string-repeat-a-string)
1921

2022
## Basic Algorithm Scripting
2123

@@ -201,3 +203,62 @@ largestOfFour([
201203
[1000, 1001, 857, 1],
202204
]);
203205
```
206+
207+
### 6. Confirm the Ending Passed
208+
209+
_Difficulty: Beginner_
210+
211+
Check if a string (first argument, str) ends with the given target string (second argument, target).
212+
213+
This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
214+
215+
---
216+
217+
#### Solution
218+
219+
```js
220+
function confirmEnding(str, target) {
221+
return target === str.substr(-target.length);
222+
}
223+
224+
confirmEnding("Bastian", "n");
225+
```
226+
227+
### 7. Repeat a String Repeat a String
228+
229+
_Difficulty: Beginner_
230+
231+
Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number. For the purpose of this challenge, do not use the built-in .repeat() method.
232+
233+
---
234+
235+
#### Solution 1
236+
237+
```js
238+
function repeatStringNumTimes(str, num) {
239+
if (num < 1) {
240+
return "";
241+
} else {
242+
let string = str;
243+
for (let i = 1; i < num; i++) {
244+
string += str;
245+
}
246+
console.log("string", string);
247+
return string;
248+
}
249+
}
250+
251+
repeatStringNumTimes("abc", 3);
252+
```
253+
254+
#### Solution 2
255+
256+
```js
257+
function repeatStringNumTimes(str, num) {
258+
if (num < 1) {
259+
return "";
260+
} else {
261+
return str + repeatStringNumTimes(str, num - 1);
262+
}
263+
}
264+
```

0 commit comments

Comments
 (0)