8000 Complete 02_repeatString · vinegraft/javascript-exercises@9856b86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9856b86

Browse files
committed
Complete 02_repeatString
1 parent b2549f9 commit 9856b86

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

02_repeatString/repeatString.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
const repeatString = function() {
2-
1+
const repeatString = function (string, num) {
2+
if (num < 0) {
3+
return "ERROR";
4+
}
5+
let result = "";
6+
while (num > 0) {
7+
result += string;
8+
num--;
9+
}
10+
return result;
311
};
412

513
// Do not edit below this line

02_repeatString/repeatString.spec.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
const repeatString = require('./repeatString')
1+
const repeatString = require("./repeatString");
22

3-
describe('repeatString', () => {
4-
test('repeats the string', () => {
5-
expect(repeatString('hey', 3)).toEqual('heyheyhey');
3+
describe("repeatString", () => {
4+
test("repeats the string", () => {
5+
expect(repeatString("hey", 3)).toEqual("heyheyhey");
66
});
7-
test.skip('repeats the string many times', () => {
8-
expect(repeatString('hello', 10)).toEqual('hellohellohellohellohellohellohellohellohellohello');
7+
test("repeats the string many times", () => {
8+
expect(repeatString("hello", 10)).toEqual(
9+
"hellohellohellohellohellohellohellohellohellohello"
10+
);
911
});
10-
test.skip('repeats the string 1 times', () => {
11-
expect(repeatString('hi', 1)).toEqual('hi');
12+
test("repeats the string 1 times", () => {
13+
expect(repeatString("hi", 1)).toEqual("h 10000 i");
1214
});
13-
test.skip('repeats the string 0 times', () => {
14-
expect(repeatString('bye', 0)).toEqual('');
15+
test("repeats the string 0 times", () => {
16+
expect(repeatString("bye", 0)).toEqual("");
1517
});
16-
test.skip('returns ERROR with negative numbers', () => {
17-
expect(repeatString('goodbye', -1)).toEqual('ERROR');
18+
test("returns ERROR with negative numbers", () => {
19+
expect(repeatString("goodbye", -1)).toEqual("ERROR");
1820
});
19-
test.skip('repeats the string a random amount of times', function () {
21+
test("repeats the string a random amount of times", function () {
2022
/*The number is generated by using Math.random to get a value from between
2123
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
2224
equals a number between 0 to 999 (this number will change everytime you run
2325
the test).*/
2426

25-
// DO NOT use Math.floor(Math.random() * 1000) in your code,
27+
// DO NOT use Math.floor(Math.random() * 1000) in your code,
2628
// this test generates a random number, then passes it into your code with a function parameter.
2729
// If this doesn't make sense, you should go read about functions here: https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/fundamentals-part-3
28-
const number = Math.floor(Math.random() * 1000)
30+
const number = Math.floor(Math.random() * 1000);
2931
/*The .match(/((hey))/g).length is a regex that will count the number of heys
3032
in the result, which if your function works correctly will equal the number that
3133
was randomly generated. */
32-
expect(repeatString('odin', number).match(/((odin))/g).length).toEqual(number);
34+
expect(repeatString("odin", number).match(/((odin))/g).length).toEqual(
35+
number
36+
);
3337
});
34-
test.skip('works with blank strings', () => {
35-
expect(repeatString('', 10)).toEqual('');
38+
test("works with blank strings", () => {
39+
expect(repeatString("", 10)).toEqual("");
3640
});
3741
});

0 commit comments

Comments
 (0)
0