Lesson 29
JavaScript Exercise: if Statement with Array Access (Phone Number Parts)
Summary
Summary of NVK Tech World Transcript
Exercise Overview
The exercise involves using control flow statements (if
, else
) and working with arrays in programming. The example provided is about handling phone numbers, specifically checking an area code from an array.
Example Array
- The array consists of three elements representing parts of a hypothetical phone number:
- Element 1:
1, 2, 3
- Element 2:
4, 5, 6
- Element 3:
7, 8, 9, 1
- Element 1:
Here, the first element 1, 2, 3
is treated as the area code.
Task
The program needs to check if the area code (the first element) is 1, 2, 3
:
- If it matches, output: “Found phone number with area code 1, 2, 3.”
- If it does not match, output: “The phone number area code does not match.”
Implementation Steps
-
Declare the Array:
let phoneNumberParts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1];
-
Check the First Element:
- Access the first element using index
0
. - Use strict equality (
===
) for comparison.
- Access the first element using index
-
Write the Conditional Logic:
if (phoneNumberParts[0] === 123) { console.log("Found phone number with area code 1, 2, 3."); } else { console.log("The phone number area code does not match."); }
Further Example
- If the array is changed to
[7, 8, 1, 9, 8, 7, 6, 5, 4, 3]
, the condition will evaluate to false:- Output: “The phone number area code does not match.”
Conclusion
The lesson emphasizes the usage of conditional statements and arrays, providing foundational knowledge for further programming exercises.
Video Transcript
Welcome to NVK Tech World. I have an exercise for you and that's gonna involve
control flow of the NIF, ELSE block and arrays. So let's suppose we have an array
with three elements and each element is a number. Let's think about something
like phone numbers. So say we have this array right here. It's an array with three
elements. First element is 1, 2, 3, number 1, 2, 3, then second element number 4, 5, 6,
third number is number 7891. So let's imagine these are parts of a phone number.
So you're processing many phone numbers in your program and you want to check for
the area code. I'm just making these numbers up by the way. They're not
necessarily something specific. So let's say the first element of this array, the
whole array itself is a phone number and this is the part of the phone number
that we're interested in. The first part is going to be the area code. In this case,
this is the area code 1, 2, 3. Now suppose we want to check in our program if we
want to check to find if the phone number is 1, 2, 3. We want to display, do
something. In this case, we're just going to display a message saying the phone
number is 1, 2, 3, something like that. Found phone number 1, 2, 3. Otherwise, we
don't really care. We're just going to say this phone number just doesn't match 1, 2, 3.
So let's add, store this array in a variable. I'm going to use let in case I need to change it later.
Let, let me say, let's call it phone number part about that. So this variable phone number part,
I use the camel case naming convention by the way, if you haven't forgotten,
naming camel case always starts off lowercase for the very first letter, but every
following new word, the first letter has to be capitalized. All right. Now this
variable holds this array of phone number parts. We're interested in the first element of the array,
which is the area code. Like I want you to right here make
console log found phone number one of area code 1, 2, 3. If the phone number parts,
the given phone number parts in this case, I already defined it here. If it is, you assume it's going to
be stored in this variable, by the way, doesn't have to be this value, I'll be changing it.
So it's changeable, but use the same variable name. Then if it's, if you find that guy to be the value
one, two, three, print out a message found phone number area code one, two, three. Otherwise,
if you did not find one, two, three for the for the area code, just say a message.
The phone number area code does not match. Okay, so that's an exercise for you.
Just as a hint, you're going to use the if statement.
You can pause the video and trade out. And then we're going to do the solution after that.
So how did it go? So we have to check first.
If this area code is 123, how to do that, we're going to do a comparison. So phone number parts
is the variable that holds all these three elements. How can we get the first element?
This is an array. So to get access elements of an array, you can use the brackets square brackets,
right after the name of the array, and you have to give it the index index always counts from zero.
So the first element here has index zero. And then this guy has index one and this guy index
two and so on. So we are only interested in the first one. So it's going to be zero.
There you go. So we grab 123. And now what we're going to do is we have to check if this guy is
indeed 123. We're going to make a comparison of equality. We're going to do triple equals.
I always use triple equals initially. I always want to test for strict equality.
In rare cases, do I want to use the double equals? Okay, it's just to make it strict. It's better and
causes you to have less bucks down the less bugs down the road. So be aware that I will always start
with the triple equals to be a strict equality. So we're going to check if that guy is the number 123.
Well, this case is going to be true, right? But it might not be true if phone number part holds
different phone numbers. Okay, so let's have this condition now as part of our if statement. We're
going to say if open parenthesis, this is the condition close parenthesis, then open the if block
with curly braces, then add some mediation to space in my by convention for me.
Then you're going to say found phone number with area code 123, something like that.
Now, if this condition is true, this will execute. But what if this guy holds different values?
So we want to take care of that as well. We're going to use the else
block right here, in case this condition turns out to be false. We're going to say that the phone
number did not match. Phone number area code did not match something like that.
Okay, so we expected to run what because this guy we know, given the value that I set right here,
it's going to find 123 for the first element. So it's going to execute this guy and skip the else block.
So found phone number area code 123. Now if I change phone number parts to be something else,
a new part array of phone number parts, let's say 781 sorry, 781 987 6543, I'm just making these up.
So let's say we have now these phone number parts is a different value. Say somehow your program is
going through this list of phone number parts, all these phone numbers. And I just change it manually
so you can see but the program will do it automatically, but we don't know how to do that yet.
But let's just try the RF statement again. So with these values 781 987 6543,
we expect our condition right here first you're going to get to this array. Check the first guy is
going to be 781 right 781 triple equals 123. That's going to be false because this condition is false.
It's going to go to the else block. Everything in the else block will be executed in this case is
going to be the console log saying that phone number area code did not match. Let's try it out.
There you have it phone number code did not match. All right.
So that's it for our exercise in this lesson. And see you next time. Bye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: