Bracket Notation -- not understanding how I am wrong ... or what is it asking for?

Tell us what’s happening:

Basic JavaScript: Use Bracket Notation to Find the Nth-to-Last Character in a String

You can use the same principle we just used to retrieve the last character in a string to retrieve the Nth-to-last character.

For example, you can get the value of the third-to-last letter of the var firstName = "Charles" string by using firstName[firstName.length - 3]

Use bracket notation to find the second-to-last character in the lastName string.

Hint
Try looking at the thirdToLastLetterOfFirstName variable declaration if you get stuck.

Your code so far


// Example
var firstName = "Ada";
var thirdToLastLetterOfFirstName = firstName[firstName.length - 3];

// Setup
var lastName = "Lovelace";

// Only change code below this line
var secondToLastLetterOfLastName = lastName;
console.log(lastName[secondToLastLetterOfLastName.length - 2]);
console.log(lastName[lastName.length - 2]);
console.log(secondToLastLetterOfLastName[lastName.length - 2]);
console.log(secondToLastLetterOfLastName.length - 2);


Your browser information:

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

var secondToLastLetterOfLastName = lastName

This is saying that the value of the second to last letter is “lovelace”, not the second to last letter

but its asking me for the second to last letter of the last name @DanCouper

But you are not assigning it to the variable secondToLastLetterOfLastName

i feel really stupid right now…
this is what I have… I guess im not sure what it is asking for

Use bracket notation to find the second-to-last character in the lastName string.

Hint
Try looking at the thirdToLastLetterOfFirstName variable declaration if you get stuck.

var secondToLastLetterOfLastName = lastName;

console.log(lastName[lastName.length - 2]);

console.log(lastName[secondToLastLetterOfLastName.length - 2]);

console.log(lastName[lastName.length - 2]);

console.log(secondToLastLetterOfLastName[lastName.length - 2]);

console.log(secondToLastLetterOfLastName.length - 2);

console.log(lastName.length - 2);

console.log(lastName[secondToLastLetterOfLastName.length - 2]);

console.log(lastName[lastName.length - 2]);

console.log(secondToLastLetterOfLastName[lastName.length - 2]);

console.log(secondToLastLetterOfLastName.length - 2);

console.log(secondToLastLetterOfLastName);

All your console logs are useless right now, you need to assign the required value to the variable secondToLastLetterOfLastName, you need to change just that line

You need to give to the variable secondToLastLetterOfLastName the required value, right now the value is just lastName

@ilenia it says Use bracket notation to find the second-to-last character in the lastName string.

Exactly, and you need to assign that value to the variable secondToLastLetterOfLastName - even the name of the variable say so. Just change this line to give to the variable that value

var secondToLastLetterOfLastName = lastName;

If you want to check the value of the variable you can use

console.log(secondToLastLetterOfLastName)

The tests are checking the value of the variable secondToLastLetterOfLastName, not what you print to the console, so you need to give to that variable the value asked in the exercise

And in your many console logs there is the right way to do it, but you are not assigning that value to anything so the tests can’t read it, and if it was a more complex piece of code it would be impossible to use that value in future steps

@ilenia Im sorry I don’t understand. I have been picking my brain for two hours
thanks for you input

@zephur

// Only change code below this line
var secondToLastLetterOfLastName = lastName; // <<< CHANGE THIS

Your code should be one line only. You need to modify lastName to get the second to last letter (which is c) and assign this value to secondToLastLetterOfLastName.

One of your console.logs in your very first post is actually correct, but you need to assign this value to the variable, not log it to the console…

1 Like

so var secondToLastLetterOfLastName = 8; ?

var secondToLastLetterOfLastName = lastName[lastName.length - 2];

2 Likes

Yaaay, you got there! :smile: