Build a Teacher Chatbot - Step 13

Tell us what’s happening:

got me on this one. just can not pass…

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.string.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/135.0.0.0 Safari/537.36

Challenge Information:

Build a Teacher Chatbot - Step 13

I’m looking on the web for example’s but all that is coming up is… charAt() or slice(). thank for yalls help…

you are assigning a string literal, is that what you want?

Just as subject[0] will access the first character of the subject string, you can find the index of the last character of a string by using the length property to find the end of the string.
For instance, if a string is 8 characters long then its final character will be at index 7 (zero-based indexing).
So the index number of the last character in any string can be expressed as the length of the string minus 1.
You can then put this expression inside the square brackets (i.e. subject[expression]), to find the final character of the string.

let lastCharacter = “subject.[string.length - 1]”;

console.log(lastCharacter);

i have tried this way as well thats why i am confused.

and what is the value of lastCharacter? what is printed to the console? is that a single character as you want?

1 Like

Why are you using quotation marks here? As @ILM says, you’re just creating a string literal.
Also, to find the length of a string, you need to use the length property directly on the string or variable which references it (in this case subject).
Finally, when you access the index of a string, you do not preface the square brackets with a dot (period).

1 Like

value of last last Character is… subject[string.lenth - 1] the last letter. am i typing it wrong?

gotcha on the quotation marks… they are out… trying to understand bare with me…

let myString = "abcdefghijk"
console.log(myString.length) // 11
console.log(myString.length-1) // 10

console.log(myString[myString.length-1]) // 'k'
// equivalent to 
console.log(myString[10]) // 'k'

Think of the expression in the square brackets as a self-contained entity which resolves to a numerical value.

1 Like

that was by far the hardest part of this workshop OMG… thank you guys for your help. could not have done it without you…

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

//this the correct code for Build a Teacher Chatbot - Step 13

let lastCharacter = “t”;
console.log(lastCharacter

The workshop is telling us to locate the last [-1] in subject variable which is “t”.
Thank you :smiling_face: