I need help with Console.log length on problem 25


#1

When I do the problem it says that " It looks like you didn't log the length of myCountry to the console"

How do i log the length of the equation? Here is my work.

var myCountry = "United States"
console.log (myCountry.substring(0,13))
// Use console.log to print out the length of the variable myCountry.
console.log(myCountry.substring(0,13));

// Use console.log to print out the first three letters of myCountry.
console.log( myCountry.substring(0,3));


#2

Strings have a .length property, that is what the exercise is trying to get you to use.

Do you remember seeing that from previous exercises?


#3

Okay I understand what I need to do.
I don't remember seeing the mycountry.length thing before in the chapters but thanks for the help.

Honestly I still don't understand what it does though, can you explain it to me?


#4

I will try :smile:

Everything has properties, even you have properties. LIke height, hair colour, likes/dislikes, etc.

In JavaScript, lots of things have properties too.

strings, which is another word for a bunch of characters, have properties.

One of those properties is length.

"apple".length will give you 5
"a great big string".length will give you 18 (if I counted right)

You can test these sorts of things over here https://repl.it/

When you wrote this: var myCountry = "United States", you set the variable myCountry to the string "United States".

So now you can ask for the length of that variable.

myCountry.length will give you 13
"United States".length will give you 13 too

Try this:
"myCountry".length -- what does that give? Do you see why?


#6

Ok, try: var myCountry = "United States"
console.log("United States".length );
console.log("United States".substring(0,3 ));


#7

The idea in this exercise is to learn to use variables instead of hard coded strings. That way you don't have to look all through your code to make a change, you can just change it in the one place.

Suppose you had a game that used the player's name, do you see the problems that you would have if you hard coded the player's name all through the game? Instead, set a variable to the player's name and then use the variable from then on.

So, you've taken the trouble to successfully set your myCountry variable to the string "United States", for the next two lines use your new variable instead of falling back to the string.


#8

I have the same problem, thanks so much for addressing the question and providing answers!