Skip to content

Commit 9de68fd

Browse files
committed
Complete repeatString
1 parent f1054a6 commit 9de68fd

2 files changed

Lines changed: 40 additions & 33 deletions

File tree

repeatString/repeatString.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
const repeatString = function() {
1+
const repeatString = function (str, num) {
2+
if (num < 0) {
3+
return "ERROR";
4+
}
5+
let result = "";
6+
for (let i = 0; i < num; i++) {
7+
result += str;
8+
}
9+
return result;
10+
};
211

3-
}
4-
5-
module.exports = repeatString
12+
module.exports = repeatString;

repeatString/repeatString.spec.js

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

3-
describe('repeatString', function() {
4-
it('repeats the string', function() {
5-
expect(repeatString('hey', 3)).toEqual('heyheyhey');
6-
});
7-
xit('repeats the string many times', function() {
8-
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
9-
});
10-
xit('repeats the string 1 times', function() {
11-
expect(repeatString('hey', 1)).toEqual('hey');
12-
});
13-
xit('repeats the string 0 times', function() {
14-
expect(repeatString('hey', 0)).toEqual('');
15-
});
16-
xit('returns ERROR with negative numbers', function() {
17-
expect(repeatString('hey', -1)).toEqual('ERROR');
18-
});
19-
xit('repeats the string a random amount of times', function () {
20-
/*The number is generated by using Math.random to get a value from between
3+
describe("repeatString", function () {
4+
it("repeats the string", function () {
5+
expect(repeatString("hey", 3)).toEqual("heyheyhey");
6+
});
7+
it("repeats the string many times", function () {
8+
expect(repeatString("hey", 10)).toEqual("heyheyheyheyheyheyheyheyheyhey");
9+
});
10+
it("repeats the string 1 times", function () {
11+
expect(repeatString("hey", 1)).toEqual("hey");
12+
});
13+
it("repeats the string 0 times", function () {
14+
expect(repeatString("hey", 0)).toEqual("");
15+
});
16+
it("returns ERROR with negative numbers", function () {
17+
expect(repeatString("hey", -1)).toEqual("ERROR");
18+
});
19+
it("repeats the string a random amount of times", function () {
20+
/*The number is generated by using Math.random to get a value from between
2121
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
2222
equals a number between 0 to 999 (this number will change everytime you run
2323
the test).*/
2424

25-
// DO NOT use Math.floor(Math.random() * 1000) in your code,
26-
// this test generates a random number, then passes it into your code with a function parameter.
27-
// If this doesn't make sense, you should go read about functions here: https://www.theodinproject.com/courses/web-development-101/lessons/fundamentals-part-3
28-
const number = Math.floor(Math.random() * 1000)
29-
/*The .match(/((hey))/g).length is a regex that will count the number of heys
25+
// DO NOT use Math.floor(Math.random() * 1000) in your code,
26+
// this test generates a random number, then passes it into your code with a function parameter.
27+
// If this doesn't make sense, you should go read about functions here: https://www.theodinproject.com/courses/web-development-101/lessons/fundamentals-part-3
28+
const number = Math.floor(Math.random() * 1000);
29+
/*The .match(/((hey))/g).length is a regex that will count the number of heys
3030
in the result, which if your function works correctly will equal the number that
3131
was randomaly generated. */
32-
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
33-
});
34-
xit('works with blank strings', function() {
35-
expect(repeatString('', 10)).toEqual('');
36-
});
32+
expect(repeatString("hey", number).match(/((hey))/g).length).toEqual(number);
33+
});
34+
it("works with blank strings", function () {
35+
expect(repeatString("", 10)).toEqual("");
36+
});
3737
});

0 commit comments

Comments
 (0)