If the problem is that it’s not passing the tests for you. The answer is because the code isn’t using the .length on the lastName. It’s not processing and returning that in the code. call lastName.length inside a console.log() means it just displays the outcome in the console.
We all know that what is in the right of the assignment operator done first then the Value saved in the variable … so we here want to assign “lastName” length in the “lastNameLength” variable … so we first go to get the length by adding .length to the “lastName” and assign it to lastNameLength … that can be done like that lastNameLength = lastName.length; … then we can console.log the variable that contains the length … console.log(lastNameLength); just like that … I wish I explained it well
the code not passing test case is just because it is expecting ‘lastNameLength’ should be assign by correct string(lastName) length.
Your code didn’t have issue its just test case (see 2 TC),if you want to log also following will work
lastNameLength = lastName.length;
Console.log displays, or shows you what the code is doing, it’s a bit like a window into what certain operations do or would do.
The challenge wants you to basically add .length to the code that is already there.
For example:
// Setup
let lastNameLength = 0;
const lastName = "Lovelace";
// Only change code below this line
//lastNameLength currently = 0
lastNameLength = lastName.length;
//Here we change the variable lastNameLength = lastName.length
// then if you want to see it in the console you can call a console.log()
console.log(lastNameLength)
In your code the console.log(lastName.length) doesn’t assign any value to anything, it doesn’t assign anything to the variable lastNameLength it just shows you what lastName.length is. It’s a useful tool for debugging
You could also reassign the variable lastNameLength inside a console.log() to have it shown in the console, but it’s not best practice
i was thinking same, but i’m not sure it’s accurate. it’s the best way to think about it at this stage i suppose.
a console log will run code if you call a function, but it just displays what’s returned from that function. if the function happens to change something in the code that will stand. i think.
in this case the console log calls the .length function but that returns the corresponding length without assigning it to a variable.
although i may be wrong in calling .length a function itself. i’m n00b.
Yeah, for sure you could reassign the variable within a console.log(), but I wouldn’t do that. It’s not a good practice, as you’ll likely want your variables and any changes you make to them to persist beyond console.logs, which are often changed or deleted over time