Welcome back.
How's it going in this lesson, we are going to fix the problem that we experienced in the last lesson
so we saw that evaluate is gives us the ability to evaluate the string and allows us to do the calculations
so we get the string value that's contained within our output screen and then we can apply some type
of evaluation to it.
The problem came when we tried to apply different operators.
We see we throw an error.
So that means that we need to look and check to see if the lost value in our string is one of the operators.
And if it is, then we can't evaluate or we need to remove out that current string value and before
we do anything or we can evaluate it, but we need to remove that out.
And the way that we can do that is using substring.
So substring method returns the part of the string between the start and the end indices or the end
of the string.
And this is ideal because what we want to do is we want to return back the end of the string so we can
see where we've got a starting position for the substring.
And when it's outputting here, when it's using the string called Mozilla, it's outputting ause so
starting position is one and it's returning back all the way to two, but not including three.
And then here it's starting at two and it's doesn't have an end.
So the substring just continues till the end of the string value.
So what we need to do from all of this is we need to simply pull out that last character.
So going back into our script, let's refresh and we'll minimize this back down.
Open up our editor and let's get our last string value because we want to check to see if it's zero.
So checking to see if and first of all, let's create a variable.
We can just call it last character and taking the my calculation string so that remember, this is that
output, the entire text of that output screen and using substring and there's a formula here that's
being shown.
So what we want to do is we want to return back only the last character and the way that we can do that.
And we saw that as well.
Within that example at the Mozilla Developer Network where we take the Mekaal and because we've using
the string methods, we can get the length of what's available in the Michael.
So we know how many characters are in the string and if we subtract one, that we get the index value
of the last character.
So let's see what happens.
So we've got our last character and we want to output our last character.
So we see that's outputting our last character.
So whatever our last character is, we see it's being output there.
So next, what we need to do is check before we evaluate, check to see what the last character is.
And that means another condition within this condition.
Checking to see if the my operator.
So just as before, just as above includes and will check to see if it includes the last character and
if that last characters there, then that means that we need to abort and we can't evaluate that string.
Otherwise we're free to evaluate.
And if it is there and if it is the last character that what we want to do is take my cowl and we want
to subtract that last character out.
So that means that we equal Michael to Michael and using substring once again.
And this time we're going to do the opposite.
We're going to get the string starting at zero position, taking the Michael length minus one.
So that will effectively remove that last character.
So I think we're ready to try that out.
So before we had the error where we had different operators at the end.
So now when I've got a plus operator, I can do a minus operator multiplication and they're not interfering.
I can switch them and go in between them and it's not throwing any errors.
So this is actually exactly what we wanted.
And next, we need to still evaluate out and do the equals sign.
So that's another thing that is still missing and we need to evaluate the equal sign.
So that's coming up in the next lesson where we're going to do the evaluation for the equals sign.
So go ahead and add this in practice Substring and added into your project.