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 JavaScript Foundations Strings Methods

Help Me Please

I am stuck on question three. I don't under substr method. I have tried still can't get it right. the question is update the variable of 'wordBrown' on about line 21, to extract the substring of "brown" from the string 'quick'.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Strings</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Strings: Methods</h2>
    <script>
      var quick = "The quick brown fox jumps over the lazy dog";

          var quickLength    = quick.length;
          var indexOfBrown = quick.indexOf('brown');
          var tenthCharacter = quick.charAt(3);
          var wordBrown = quick.substr(2);
          var quickUpper = quick;
          var quickLower = quick;
    </script>
  </body>
</html>
Vince Brown
Vince Brown
16,249 Points

Hey layeesolo, The .substr() method takes 2 parameters in this case the first parameter would be the point to start extracting from and the second is the length of the extraction.

So if you were trying to update wordBrown to be "brown" by extracting "brown" from var quick it would be

var wordBrown = quick.substr(10,5);

Since js index starts at 0 the extraction would start at the 10th position and since "brown" is 5 letters long the second parameter would be 5.

I hope that clears things up for you.

Cheers, Vince

2 Answers

Use the substr(index, length) method to extract brown from quick. What you must do is start at the very beginning of the string of quick which is index 0. We count every character including spaces so "T" is 0, "h" is 1, etc. until we count up to the b in "brown" which is index 10. So, index 10 is where we start. We want to extract "brown" which is 5 characters long so we use 5 for our length. Thus:

var wordBrown = quick.substr(10,5);
<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Strings</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Strings: Methods</h2>
    <script>
      var quick = "The quick brown fox jumps over the lazy dog";

          var quickLength    = quick.length;
          var indexOfBrown = quick.indexOf('brown');
          var tenthCharacter = quick.charAt(3);
          var wordBrown = quick.substr(10,5);
          var quickUpper = quick;
          var quickLower = quick;
    </script>
  </body>
</html>

If we wanted to extract the beginning "The" from the quick string, we would start at index 0 because it has the "T" and go 3 length because "The" is 3 characters long.

var wordThe = quick.substr(0,3);

thanks. I understand it now.

If you feel my answer is worthy of being the best answer, you can mark my answer as Best Answer. Thanks, layeesolo!