Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmadison briggs
7,085 PointsWhat is the -1 for?
I don't understand the first line of code in the error handler for the vote up/vote down. This line: if(req.params.dir.search(/^(up | down)$/) === -1 {
1 Answer
Martin Lecke
14,385 PointsThis code line is using the string .search()-method. If there is no match found when using the method the number returned is -1
This is because .search() is returning the position number. So if the method doesn't find a match it cannot return 0
because that's also the first number in the string.
Try it out in your console
"madison".search('i');
// will return number: 3
"madison".search('M');
// will return number: -1 because it doesnt exist in the string
madison briggs
7,085 PointsOk, great. Thank you!
Saud Tauqeer
Courses Plus Student 8,099 Pointswhat about the ^ and $? and the / \
Ovidiu Cotorogea
12,998 PointsIt is a regex expression, The ^ marks a beginning of a string and $ marks the end of a string. You usually insert a regex expression inside '/' like /someExpression/
. Check out this -> https://www.debuggex.com/cheatsheet/regex/javascript
Jean Pumarol
22,747 PointsJean Pumarol
22,747 PointsYou also can use another method that is available in the String object. The method is called endsWith. There are other two methods called startsWith and includes. This methods make finding sub-strings extremely easy. Hope that helps.
Documentation: MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith W3Schools - https://www.w3schools.com/Jsref/jsref_endswith.asp