Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ex1/ex1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@
* fn(",,,") === 2
* fn("hel,lowo,rld") === -1
*/

function getThirdCommaIndex(str,searchFor) {

var index = -1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial value of -1 isn't being used

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put a comment about why the value -1 is important.


for (var i = 0; i < 3; i++) {

index = str.indexOf(searchFor, index + 1);

// If we did not find a comma in str will stop searching
if (index == -1) {
break;
}
}
return index;
}

module.exports = getThirdCommaIndex;
19 changes: 19 additions & 0 deletions ex1/ex1Spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var getThirdCommaIndex = require("./ex1");

describe("function getThirdCommaIndex", function() {

it("returns -1 when no third comma", function() {
expect(getThirdCommaIndex('hel,lowo,rld', ','))
.toBe(-1);
});

it("finds a comma in a regular sentence", function() {
expect(getThirdCommaIndex('hello,world,this,is,a,great,day', ','))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maximum line length on github.com is 80 character, so this line gets too long. You could break this into two lines, but I think it might be easier to use a shorter tab (maybe two spaces instead of an entire tab? See https://www.google.com/search?sourceid=chrome-psyapi2&ion=1&espv=2&ie=UTF-8&q=vim%20tabs%20to%20spaces&oq=vim%20tab&aqs=chrome.1.69i57j0l5.3624j0j7

.toBe(16);
});

it("works even when there are no characters beside commas", function() {
expect(getThirdCommaIndex(',,,', ','))
.toBe(2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you do break a line because it is long, you need to push the continuation line in: +tab.

});
});