Find the Length of a String -2019-03

Tell us what’s happening:

I don’t know what the problem with this code? Please help me. Thank you.

Your code so far


// Example
var firstNameLength = 0;
var firstName = "Ada";

firstNameLength = firstName.length;

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

// Only change the code below this line.

 lastNameLength = lastName.length;


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

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

1 Like

They want you to use “.length” which I believe is a built-in method. Instead you set lastNameLength = 8; which isn’t what we want because we want the lenght of “Lovelace” specifically if that makes sense.

Now, to explain why they want you to use that specifically, I’ll use an example, Imagine you let a user enter their name and you watch to know how long their name in terms of characters, well if they enter “Ali” it will be 3 but you set lastNameLength = 8; which isn’t right.

2 Likes

To be more specific you are failing the test because you changed code you were not supposed to.

// Only change the code below this line.

while you are setting lastNameLength to the appropriate value you have also changed the initialization value of lastNameLength to 8.

You can test this by creating a variable and setting it to 0. e.g., var initial = 0; then assigning initial to lastNameLength. You will fail the test even though technically the value of lastNameLength is still 0.

if changing the initialization value of lastNameLength was in error then reset the test. the final assignment you have made is correct otherwise.

2 Likes

var e = 11;
var f =“GoodMorning”;
if (e === f.length){ alert (‘True’); } else {alert (‘False’);}

run this code in your console
you will come to know.

Thank you

2 Likes

I understood where I did wrong. Thanks a lot.:grinning:

1 Like

Thanks a lot…:grinning: