Arrays in Action
Last task in this round. we will explore the array and see it in action while updating some of the content on our live page example.
Video Transcript
00:00All right, fantastic.
00:01So we've seen how to work with the shortcut object,
00:04and we actually started to integrate it
00:05into our application.
00:07What I want to do now is let's do a quick, similar thing
00:10for our early bird on time and late prices.
00:12What I want to do is I want to put it out dynamically.
00:15Imagine, now, in an ideal world that this probably
00:19would be done on the server side in PHP.
00:22Lucky for you, the differences between JavaScript and PHP
00:25on the basic core stuff are not that huge.
00:27So if you decide to learn a bit of PHP, truly encouraged.
00:33It's really not hard.
00:34It's very similar to the stuff that we're
00:36learning right now as well.
00:37The syntax is just a little bit different.
00:40So I'm going to create an array for prices.
00:43And the array for prices is basically
00:44going to be a short-handed array, but the short-handed array
00:48is basically going to have a few different properties.
00:51So what I want to do is maybe I'm
00:52going to create a function that creates that shorthand
00:55function for us.
00:56The reason why I'm doing that is just for practice.
00:59So I'm going to create a function,
01:00and I'm going to call this price object, create price object.
01:07By the way, the reason why I'm putting the word create
01:09before is because ideally, when you call function names,
01:12try to give actions in the names of your functions,
01:14because functions are actions.
01:16So by giving an action, it works better.
01:20It's easier to read than price.
01:22Sounds like a property?
01:23Well, create price indicates what it actually really does.
01:27So create price will accept a couple of parameters.
01:30Those parameters, if we look into the live site for a second,
01:33and I'll just jump into the browser,
01:35I'm going to want to use a title,
01:38or I'm going to want to use a price,
01:41and I'm going to want to use a date as well.
01:43So let's make this happen.
01:45So back into our code, we said we're going to create a title.
01:51We're going to create a price, and we're
01:53going to create, last but not least,
01:55we said we're going to create here a date as well.
01:58And it's time for us to create this price bucket.
02:02And by the way, homework for you, I
02:03know we worked with hard set dates earlier, which
02:07we've noticed in the early date and the late date.
02:10Revisit what we did, fix it to match up to whatever prices are
02:13dynamically, and whatever dates are dynamically created
02:17in our array, and now that we're
02:19going to create with our create prices,
02:20like that will be a really fully fledged application.
02:23All right, so let's create our object.
02:25First of all, I'm going to create that object itself.
02:28I'm just going to create even without the variables
02:30for a second.
02:30First of all, title is going to be title.
02:33Our price is going to be the price.
02:36Our date is going to be the date.
02:38Done, we've done it.
02:42So last but not least, we want to maybe return it.
02:45So we're going to return that value all back.
02:47It looks kind of ugly, but it works.
02:50All that's left for me to do is now start creating the prices.
02:53Now, obviously, in the real case of now,
02:55we might not need to call a function.
02:57And actually, from a computer processing,
03:01a function defeats the purpose here
03:03because we're adding more complexity in a place
03:05that doesn't need complexity.
03:07But for our sake and purpose, we don't care.
03:10So let me just find the prices here for a second.
03:12And what I'm going to do is I'm just
03:13going to copy those copies that we have right there.
03:16And we're going to start off with the, whoop,
03:19we're going to start off with the early bird.
03:21We're going to move on to the actual price itself,
03:24which was 275 here.
03:30Oh, we could leave that as a number.
03:32And last but not least, we want also our date,
03:35which was 315.
03:37And by the way, I'm not making your life easy here.
03:39If you do go through creating the real thing,
03:43you're going to have to figure out a way
03:44to extract this information.
03:46The 315, which is actually the 215,
03:49if you remember, because it starts from zero.
03:52All right, so we have here the first create.
03:54And look how I'm using a function
03:55inside of this beautiful thing that we have here.
03:59So one, two, three, we're going to call all three.
04:05Time for us to call the on time price.
04:07After that is going to be the late price.
04:09So there's the on time price.
04:12There's the late price.
04:17Was it price or was it late bird?
04:19Yeah, price makes more sense.
04:21375, 450.
04:24375, 450.
04:29And last but not least, we need our date, 415.
04:33And this one doesn't have a date,
04:38but we probably should have a date
04:39because it would be like the date
04:40where it's actually completed.
04:42So let's say 415, and then the event itself
04:45is actually going to happen on 5.1.
04:47All right, even though it's not written there
04:48at all at this stage.
04:50All right, so we have all our different dates,
04:51although we're not using them in all three of them.
04:54And now that we have it created,
04:56it's again down to actually adding them
04:59and creating them in a place where it'd be dynamic
05:01instead of having it static.
05:02So there's a few smart ways of doing this.
05:05And I want to show you just one of them.
05:07And we didn't learn yet about arrays.
05:09So I'm not going to, sorry,
05:10we didn't learn yet about loops.
05:12So I'm not going to introduce loops yet.
05:14I'm just going to show you how to do one of them.
05:16After we do that, we might revisit later on
05:18and see how could we do it better, all right?
05:21So in this scenario, I'm just going to approach directly
05:24our object here, and I'm just going to go to our title
05:27and I'm going to call it, I'm going to give it an ID,
05:29and I'm going to call it price one title, right?
05:38We're going to do the same thing for our ID.
05:43And by the way, this is the absolute wrong way
05:45of doing this.
05:46And I'm going to expect you to do it better.
05:48But for the level that we're in right now
05:51and for the practices that we're doing right now,
05:53this is really all we need to do.
05:55So I'm going to put your price.
05:57And the reason, by the way, it's not a good way of doing it.
05:58Every time when you associate something too explicitly,
06:02it makes it less flexible.
06:03And we have a format that repeats itself three times,
06:06almost, perbatim.
06:08And that almost, it actually does repeat itself, literally.
06:12So really, if we really want to be smart about this,
06:15how about, let's be smart about this.
06:17Instead of not being smart about it.
06:20Now, if we take a step back here for a second,
06:24and I'm going to undo, undo, undo, undo all these changes
06:26that I made, and I get rid of almost everything, right?
06:30Well, let's redo from our last example.
06:33Perfect.
06:34All right, so the only difference is here,
06:36there's different copy, right?
06:38We have the early bird.
06:39We have this.
06:40We have this full copy that changes between each one.
06:43If instead of having the date directly inside,
06:45maybe we would have the sub copy.
06:48If we would have the sub copy,
06:49we wouldn't really have almost any differences
06:51between the three.
06:53And then we could have approached our div layer,
06:55this div layer, and replace everything within it.
06:59True?
07:00Very true.
07:01And we could have left it empty.
07:02All right, so I'm going to copy,
07:04let's revisit this logic for a second.
07:07For our date, instead of really putting the date,
07:09we're going to put inside of here,
07:10whatever is in that line.
07:12So I'm going to go back into that source code,
07:15and I'm going to move into the second line.
07:16Now, I don't really care about the date.
07:19Obviously, if you try to fix and modify the stuff here,
07:22you should care about the date.
07:23So when you're done with the course,
07:25you could always revisit this live example
07:28and make it better, because it's not a final project.
07:30It's an almost final project
07:31ready for you to play with JavaScript.
07:33All right, so we got all our copy in here.
07:35Probably the name date is not really that great now,
07:38so we should call it probably subtitle.
07:42And as a subtitle, we want to create a property subtitle
07:45as well, there we go.
07:46So we just fixed everything we had to fix.
07:48Now that we've fixed everything,
07:49we could revisit our source code,
07:51and this time around, instead of focusing one by one,
07:53creating a variable for each,
07:55we could create an ID here,
07:57so we could give your NID
07:59and give it a more generic global name.
08:01So our generic global name will be price one,
08:05and we'll just have price one, price two, price three,
08:07in this case, and scenario.
08:10And we will look at other better ways
08:12of doing this momentarily.
08:15All right, and we'll do your price three.
08:19All right, so we have here the price one,
08:20price two, price three.
08:22Now we could, in theory, just access the price itself
08:24and recreate everything that's inside here.
08:27So let's see how that's done.
08:27Let's start with price one.
08:29So if I go in here and I go back to my,
08:31and I'm gonna create document,
08:35get element by ID,
08:38and I'm gonna get the element of price one,
08:42and I wanna go to my inner HTML,
08:45and I wanna replace that inner HTML,
08:47and you know what, let's create a function
08:51called generate price HTML.
08:56All right, our generate price HTML,
08:58we probably would wanna send into it which one we want,
09:01so let's just send into it zero
09:03because we want position zero in the array
09:05to do it for price one.
09:07So I'll create quickly that function.
09:10Again, we're gonna call it generate price HTML.
09:14It's gonna accept the, well,
09:16let's call this the I position.
09:20Now all that's left for us is to actually return
09:22that combination of copy,
09:24and to do that, I'm just gonna go into my source here,
09:26copy everything that's in here,
09:30put it inside of a quotation mark,
09:34and now that's inside of a quotation mark,
09:35first of all, in HTML, the line breaks are really unneeded.
09:39If we really wanted just for the sakes of visuals,
09:42we could close the quotation,
09:48add a plus, and just do line by line.
09:51This is just for visuals
09:52just to make it easier for us to see.
09:55It has no actual,
09:57it doesn't really matter too much on the processor.
09:59This is really just for you if there's too much copy,
10:02and you just want it to make it more visually easy
10:04to work with.
10:05By the way, speaking about that,
10:07just one more thing about this,
10:08so now that we have this,
10:09and obviously this is not the full featured thing
10:12that we actually would wanna do,
10:13but if we go and look at our design for a second,
10:16we should get everything working right.
10:18Our copy here is true to the nut.
10:21It's absolutely completely right,
10:23and on top of that also,
10:25with our copy up here,
10:27not only that it's right,
10:29just to make sure that you know that it's right,
10:30I'm gonna delete everything that's here
10:32just so you could see that it actually really does work,
10:35and it's doing everything that it's supposed to do.
10:37So if I save this and I go into the design,
10:39although we changed price run one right now,
10:41we'll see that all the content appears
10:43and comes in without any issue still.
10:45All right, so back into this,
10:47I just wanna say one more thing.
10:49If, for example, you wanted to put the early bird
10:51inside of a double quotation mark as well,
10:54but you're using already a double quotation
10:56around your string, you're kinda stuck, right?
10:59This will break and not work.
11:00It's gonna give you errors.
11:02To fix that, all you have to do is put a,
11:05basically a backslash,
11:07a backslash right before the double quotation
11:10that you don't really mean it to be a double quotation.
11:12By doing that, you're gonna fix that issue,
11:14and now it's gonna run in your code without any problem,
11:17and now you're gonna have an early bird
11:18in a double quotation,
11:20aka not really early bird,
11:22which fits perfectly for our fake example.
11:25Now, the only thing that's left for us
11:27is actually to put in the real copy
11:29that's inside of position I.
11:31So let's do that.
11:31First of all, we actually do wanna
11:34stop our copy right here, right?
11:37And we wanna actually add inside to it
11:40the actual A prices value of position zero.
11:45In our case, it's actually I,
11:47even though it's position zero,
11:48because we're getting that I
11:49when we're calling that function,
11:51we're getting that zero,
11:52and then we could put in our title.
11:54And we're gonna, let me just copy this.
11:56I'm gonna copy this whole batch here,
11:59and I'm gonna leave that dollar sign,
12:01copy all this good stuff right here,
12:05and what's next here?
12:07We wanna, instead of title,
12:08we wanna put inside the,
12:12oh, we didn't do a price one.
12:14Oh, did we?
12:15Oh yeah, we did, sorry.
12:16So here is our price, right?
12:18I'm completely floating right now.
12:21And last but not least,
12:23we wanna do the same thing here,
12:25and instead of price,
12:26we wanna put here our subtitle.
12:32If we run this baby one more time,
12:34and we look at it in the design view,
12:36we're gonna see that everything is still working,
12:38which is a beautiful indication to us
12:40that the rest should work as well.
12:42And if we run this just one more time,
12:44this time around, we're gonna go also for price two,
12:48and we're gonna send in item one.
12:51We could do it also for price three,
12:54and send it to item two.
12:56And now just to see that it actually is working,
12:58I'm just gonna change the price here to $500.
13:01I'm gonna change this one to be $1,000,
13:04and I'm gonna change this one to be $10,000.
13:10Run our design,
13:11and now we could see that our price is updated.
13:13Obviously, our format maybe would be smarter
13:15to have a comma here.
13:16Not a big deal, because in JavaScript,
13:18and we know that we're not actually
13:19doing anything to our copy,
13:21so we could actually make sure
13:22that our 10,000 is not a number but a string,
13:24and add that comma directly into it.
13:26Same for our 10,000.
13:29So we could put a comma in our 10,000,
13:31and we could change any copy that we want dynamically,
13:33which is obviously very powerful and very useful
13:37when you're creating something,
13:38when you need to create something dynamically.
13:40And one of the stuff that you wanna think about
13:42when you're doing these stuff,
13:43you shouldn't try to just put everything out
13:44dynamically in JavaScript,
13:46because it's gonna lower your scores on search engines.
13:49If you're doing this on a server side and a PHP,
13:51then you can do whatever you want that way,
13:53because it's gonna make everything flexible.
13:55But when you're going down to maybe dates,
13:57or you're going down to prices and such stuff like that,
13:59then you probably would wanna externalize that
14:02into JavaScript,
14:03because then it'll make it easier for you
14:04than if you need to change the price
14:06and change it in every place, every occurrence,
14:08then you could change it relatively easily.
14:10You could put a placeholder of a price inside,
14:13and then make sure that the JavaScript updates
14:15that most browsers today have JavaScript enabled,
14:18and if they don't,
14:19you probably can't make a sale anyways.
14:21So obviously, be smart about it,
14:23but don't put everything that you have,
14:25every copy that you have on a webpage into that.
14:28If you are planning on doing that,
14:30such as in our scenario,
14:31that we have some sort of a template here for courses,
14:34you would probably wanna do that in PHP,
14:36because the template,
14:37you wanna make sure that when the search engine
14:39comes onto your page,
14:40it doesn't have to worry about JavaScript,
14:42that everything that it needs to see,
14:44that everything that needs to be clickable,
14:45is clickable and is visible for a search engine,
14:48because that's just gonna improve your scores overall.
14:50But that really is all we wanted to do
14:53to practice our objects,
14:55to practice our arrays before we move on to our next topic.
