Hey, welcome back in this lesson, we're going to be looking at the switch statement and what the switch
statement does that evaluates an expression so matching the expressions value to the case clauses and
then executes the statements associated with that case.
So let's check it out and show you some examples of that.
So I've set up an input area as well as a button.
So we're just ask what time is it?
And then we've got a button.
So let's make these interactive these fields so we'll select the input area as well as the button.
So button document using query selector.
Let's select the button element so we only have the one.
So just select that first one and I'll just do another one for answer and do document and query selector
and we'll select the input.
And as we only have one input, it's going to be relatively easy to pick these up.
So adding an event listener to the button so that we're going to be listening for is a click.
Whenever it gets clicked, it's going to run a function.
And within this function, what do we want to happen?
We want to ask the question, so what time is that?
And we're going to do an evaluation on the switch statements.
We'll have the switch statement here and here.
And for now, we'll just output whatever the value of the answer and whatever the value is that's associated
with the answer.
So if there is no value for the answer, then we're not going to try to provide a solution for it.
So we need to make sure that we do have a value within the answer.
So let's try that and put in a value.
And so now we get the value output there.
So we want to make sure that whatever we're outputting, whatever responses in there, that this is
going to be an actual number that we can use.
So let's check out then let's grab that value and we're going to check to see the type that it is.
So will console log out type of and this will tell us the data type, so by default, within the input,
it's going to be a string value.
So even though we've got numbers in there, it's still going to be returned back as a string.
So we want to do is we actually want to convert it into a number.
So let's set up a variable and set up a brand new variable and using a number, let's convert the answer
value into a number and then we'll also get the type of four Antz.
So the original is always going to be a string.
So want to check to see what we're returning back and if it's a number and what type it is.
So let's try that one more time.
So we've got some letters.
So it's a string, the original ones, a string and now it's been turned into a number and string and
numbers.
So with no we're always turning it into a number, but it doesn't necessarily mean that it is an actual
value of a number.
So if we cancel out the value of ants, if it's a string, then it's actually going to be returning
back as an end.
So if we've got a string value, it's turned back as an and and if we have a number that's going to
still return it back as a numbers.
So the NUM conversion is going to either check to see if it's a number and if it's not a number, then
turn it back as not.
So that is a variable that we can use and value representing, not a number.
So if none is equal to and then that means that console log not a number and just duplicate that and
else and move that in there.
So it's aligned.
I'll get rid of that extra line and I'll just put.
OK, so let's check to see what gets returned back here.
So forty four is OK whereas.
What we're returning back, so this is being treated as a no, Sanon is being returned back as a no.
So we're still returning back.
OK, so it's not checking to see if it's value of nine.
It's just checking to see if it's not a number.
So let's update this with no and we'll just check to see if now it's going to return that and it's still
returning back.
OK, so this is a problem using the no method.
So let's use the pass integer and it's going to return it back as no value and that will give us different
values there.
So let's update this stuff.
So now we're using ETR and it's been returned back as not a number.
And now we've got a number and it's been returned back as a number of value.
So both pass integer and no, they both are converting it into a number of the difference between pass
integer is that if you have value and then if you have some numbers, it's going to return it back up
to where it sees the end of the number, whereas number is going to return that back separately.
And if we do, instead of parse integer, if we use no method in order to do the conversion and we do
two, it's going to just return it back as.
Not a number, so it's not going to take any of the additional values, whereas Pass Integer is going
to return it back as two, so there's a difference there.
And depending on what you want to use, you can use either the pass integer or the number.
So let's use the pass integer in case that there are some other values that are entered in there.
So we want to get any of the value, the number of values out of that.
So now that we've got all of that and we're still not able to check to see if this is an actual number,
if it's a valid number value.
So let's take a number and we'll see if this works as we do need to have a number of value for the switch
statement.
So let's add in a number and it's coming back, not a number and it's coming back that this is OK.
So that is OK.
So we need to negate this and then we should be good to make sure that we're checking to see if not
a number.
And now we are getting a number.
So it is OK, stay tuned for part number two.
Coming up, where we're going to look at switch statements.