Build a Teacher Chatbot - Step 13

Tell us what’s happening:

I don’t understand what I am missing. I typed the answer how the example question did, but it still has an error. Can anyone else see what I am doing wrong?

And if it isn’t too much trouble, any explanation as to what I am doing wrong and why would be super helpful!

Your code so far

console.log("Hi there!");

const botName = "teacherBot";

const greeting = `My name is ${botName}.`;
console.log(greeting);

const subject = "JavaScript";
const topic = "strings";

const sentence = `Today, you will learn about ${topic} in ${subject}.`;
console.log(sentence);

const strLengthIntro = `Here is an example of using the length property on the word ${subject}.`;
console.log(strLengthIntro);

console.log(subject.length);

console.log(`Here is an example of using the length property on the word ${topic}.`);
console.log(topic.length);

console.log(`Here is an example of accessing the first letter in the word ${subject}.`);

console.log(subject[0]);

console.log(`Here is an example of accessing the second letter in the word ${subject}.`);
console.log(subject[1]);

console.log(`Here is an example of accessing the last letter in the word ${subject}.`);


// User Editable Region

let lastCharacter = subject;
lastCharacter[lastCharacter.length - 1];
console.log(lastCharacter);

// User Editable Region

Your browser information:

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

Challenge Information:

Build a Teacher Chatbot - Step 13

what is the middle line here doing? and what are you assigning to lastCharacter?

See, my original work was

Blockquote
let lastCharacter = [subject.length - 1];
console.log(lastCharacter);

But that didnt work. So I went to the example given which lists:

Blockquote
const firstName = “Jessica”;
// returns “a”
firstName[firstName.length - 1];

So I just typed what I have listed in the above coding.

I forgot to say this is the entire step instructions.

Blockquote

Step 13

In the lecture videos, you learned how to access the last character in a string like this:

Example Code

const firstName = "Jessica";

// returns "a"
firstName[firstName.length - 1];

string.length - 1 will always give you the last index number for a string.

Create a new variable called lastCharacter and assign it the value of the last character in the subject variable.

Then, log the value of the lastCharacter variable to the console.

here you are creating an array not accessing a character from a string. To access a character from a string you are missing… the string

I noticed the “Jessica” string in the example but the assignment says to assign “subject” as the variable. They didn’t give me a string. Am I supposed to make one up?

subject is this variable at the top of the code.

const subject = "JavaScript";

You are asked to create a lastCharacter variable and assign it the last character of the string that was assigned to the subject variable, then log it out.

I’m sorry but apparently I don’t understand and I’m still lost. I’ve tried every combination I can think of and nothing is working.

Hi @calypsocodes

Please post your updated code and any error message you received.

Happy coding

The code I listed above in the original post is “officially” the code I am using. Anything else I try is just on a whim and I end up reverting back to this one in case anyone has any insight into what I am doing wrong.

Please show the code where you implement this advice, regardless if it worked or not:

Please include any error messages.

Hi @calypsocodes

In the example, it shows how to access the last element in a string.

const firstName = "Jessica";

// returns "a"
firstName[firstName.length - 1];

You need to apply the same structure to access the last element in the subject variable.

Your code is assigning the whole subject variable to lastCharacter.
You need to assign just the last element of subject to lastCharacter.

Happy coding

1 Like

I tried a dozen different things so I didn’t bother posting all of them here. The person who wrote after you was able to help me. Thanks though!

1 Like

@Teller THANK YOU! I was not understanding the instructions but what you said helped me understand 1) what I did wrong in my code and 2) what I actually needed to focus on. That is exactly what I needed.

Sincerely, thank you!

1 Like

I was stuck on this too last night. Felt even more stupid when i figured it out​:sweat_smile::sweat_smile: