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 trial

JavaScript Build a REST API With Express Building API Routes in Express Error Handling in Express

madison briggs
madison briggs
7,085 Points

What 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 {

Jean Pumarol
Jean Pumarol
22,747 Points

You 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.

var str = "Hello world, welcome to the universe.";
var n = str.endsWith("universe.");

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

1 Answer

Martin Lecke
Martin Lecke
14,385 Points

This 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

Read more about .search() on w3c

what about the ^ and $? and the / \

Ovidiu Cotorogea
Ovidiu Cotorogea
12,998 Points

It 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