Stuck in JavaScript on lastname count

Tell us what’s happening:
I have tried and deleted and searched for hints on how to do this. Should be simple, but it’s not yet. Thanks for your help.

Your code so far
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/find-the-length-of-a-string


// Setup
var lastNameLength = 0;
var lastName = "Lovelace";



// Only change code below this line

lastNamelength;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36.

Challenge: Find the Length of a String

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Here you are referencing the variable, but you are not doing anything with it. What should this line do?

I am trying to get the length count for the last name. Were you able to see the lines above this or did it not copy?

I saw the lines above that. But this is the line you need to modify.

So you need to do two things, right?

  • Get the length of the lastName string
  • Set that value to the lastNameLength variable.

So do I need to add var in front of lastname? I think I am likely missing a small thing like a period or quotations or something in the wrong place, but I can’t seem to find it. Can you please see what I did and help?

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/find-the-length-of-a-string

Do you remember how to assign a value to a variable? How would you give lastNameLength the value of 5 in that line?

Here is an example I came up with to try and get the length to return five. Is it right?

var lastName = “Carne” ;
“Carne” .length;

That’s a good start! But what if the value of lastName changes?

var lastName1 = “new name”;

*I really have no clue. I can’t see to find it on W3 either. What point am I missing?

Hi @juliakajill!

I think a visual example might help.

Let’s look at this line right here.

The string “Lovelace” has been assigned to the variable lastName. Another way to think of it is if we place “Lovelace” inside a box called var lastName.

So how can we use just the variable name (which holds the contents of “Lovelace”) and the length property in our answer.

We can also refer back to the FCC example too if that helps.
For example, if we created a variable var firstName = "Charles" , we could find out how long the string "Charles" is by using the firstName.length property.

Make sense?

I appreciate the visual and those are the exact examples I’ve been looking for a couple hours now (usually takes me about five minutes) so of course I am starting to get frustrated as I can’t see what I am missing.

To answer you example above I would type:

lastName = “Lovelace”

is that it? Or

lastName = “Lovelace”;

or maybe neither?

I’m pretty sure I’ve typed lastName.length and I am missing something like do I need var or quotes or equal sign? I really have tried every combination and searched the internet and there is no example that is turning my light bulb on unfortunately.

Can you show me the Charles example as you would exactly type it out to find the length? The way they do it hasn’t worked for me. Thanks for your help.

lastName.lenght

Is that correct?

Hi there,
Just check your spelling of length.

The example doesn’t show how they get “Charles” length count back which is why I’m stuck.

Would I type:

firstName.length

I bet when I see the error I won’t believe it.

Using javascript string.length

So I need to add console.log to the lines?

Yes that is how you find the length for a variable called firstName.

Now replace firstName with lastName and it will work.

lastNameLength = lastName; // add length property here just like in the example above

Ok please check what I did as that doesn’t seem to work either… Did I change something in the wrong place?`

type or paste https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/find-the-length-of-a-string here

you need to include your code in your post for us to be able to see it