Placeholder Image

字幕表 動画を再生する

  • the weather.

  • Where I live is absolutely crazy Right now we have thunderstorms.

  • Tornado warning snow literally.

  • Everything you can think of is going on right now.

  • So I'm thinking, Why don't we look towards Mars, see what the weather's like there?

  • Because right now I'm thinking it's probably gonna be better than here.

  • So I enlisted the help of the absolutely amazing Kevin Powell to create the HTML and CSS four this Mars weather app.

  • And today we're gonna take the Java script portion and actually make this HTML and CSS function and give us the live weather of Mars.

  • So let's get started now.

  • Welcome back to Webb.

  • Dead simplified.

  • My name's Kyle, and my job is to simplify the web for you so you could start building your dream project sooner.

  • So if that sounds interesting, make sure you subscribe to the channel for more videos just like this one to get started.

  • I have all the code that Kevin created in his videos Open up on the left hand side of the screen and the output of that code open on the right hand side of our screen.

  • And as you can see, we have this really nice looking weather app.

  • But all of the data is just placeholder data.

  • And if you are interested in figuring out exactly how Kevin created this, make sure you go over to his YouTube channel.

  • I'm gonna have linked in the description below where you can watch his videos, where he built out all of this.

  • But if you want to just jump in straight to the Java script, you can also go to the get hope linked in the description to get the exact code as you see it on my screen so you can start exactly where I am now for us, what we want to do is actually hook this up to an A P I.

  • That gives us live weather updates from Mars.

  • And luckily for us, NASA has an A p I, which is going to give us live updates straight from Mars, which actually gives us the weather on Mars.

  • And this FBI is kind of cumbersome to use.

  • It's got really annoying key names and stuff to deal with, but I'm gonna show you exactly how we can break this down and use this ap I So the first thing I want to do is just hook Ah, wrap up to the A p I and start fetching data from it.

  • And we can use this a p I A girl right here.

  • So just click on this.

  • Make sure we copy this girl because this is going to give us the data.

  • And for your purposes, you're gonna want to get a special AP ikey.

  • Right now, the a p i ke is just the demo.

  • A pikey where you can get your own a pikey by just searching for NASA a p I.

  • So now let's open up our job script file.

  • As you can see, Kevin's already done a little bit of JavaScript just to make this taco section down here actually function, so that's nice.

  • But what we want to do is actually get to the a p.

  • I working.

  • So we have that a p i euro, we're gonna call it a P.

  • I clips a p i euro and just set it to that euro that we just copied over.

  • And we're gonna take our a p i ke out of here, and we're gonna set that into its own variable, which we're gonna call a p i ke just like that.

  • And now inside of this, a PR girl pushes injector a pikey back in there by using string interpolation This way, when you update your own ap I ke all you have to do is paste it in right here, and you don't have to change anything else with this code.

  • Let's just remove some of these comments that Kevin has in here.

  • And now with that out of the way, we can actually work on Queen this ap I So what we want to do is just create a function.

  • We're gonna call this get weather and what this is going to do.

  • Is this just gonna query that a p I but using fetch.

  • So we're just gonna say, fetch here.

  • We're gonna fetch that a p I euro.

  • And then what we need to do is just call dot Then this is going to give us a response, and we just want to convert that response to Jason by calling reds dot Jason next.

  • What we can do is we can do another.

  • Then this one is going to contain our data, and this is just going to be the data from the A P I essentially all of this Jason that you see here.

  • And we're just gonna long that out for now to see exactly what we're working with.

  • So if we inspect our page goto our consul and we make sure that we call this get weather function, we should see that we have a bunch of different information being printed out straight from the A P.

  • I.

  • You can see we have information for day for 51 4 52 for 53.

  • These are called souls there the individual days of the cycle of Mars.

  • And as you can see here, we have our soul keys, which correspond to those days up here.

  • And then finally, we have this validity cheques section which, for our use cases, were just gonna ignore because we're just gonna trust the data that were given to us from the NASA ap.

  • I so in order to parse out this data into the information we want, which, as you can see in our case, we want to get these soul number, we want to get to the actual high and low temperature, as well as if I increase my screen size here.

  • You see that?

  • We want to get the wind speed and we need to be able to convert these between Celsius and Fahrenheit.

  • Everything in this a p I is going to be in Celsius.

  • So all these values air Celsius which make sure that we keep that in mind when we're creating this a p I.

  • And really all of the information we want is inside of these numbered sections for 51 for 50 to 4 53 so on.

  • So we're just gonna parse out just these numbered sections for all of the information we need.

  • So in order to do that, let's just put this off to the side.

  • So we can see are a pie data and what we need to do instead of here is get all of these different numbered sections and a really easy way to do that is by using the D constructor of property as well was the spread operator which I'll show you right now.

  • So let's just say const here and we're gonna define a bunch of different variables based off of our date a variable here, so we're gonna d structure our data into multiple different variables.

  • Our 1st 1 is going to be our soul keys, which is this property down here.

  • Our next one is going to be for our validity checks.

  • So just type in validity checks.

  • And then what we can do is we can say we want to get everything else, which is all of these different numbered sections.

  • So we could just put a triple dot here and call it whatever you want.

  • In our case, we'll call it Soul Data.

  • So this soul data variable is going to be everything in for 51 4 52 and so on.

  • Essentially everything that's not so keys.

  • And that's not validity checks.

  • This is just a quick way to get the exact the data that we want.

  • And if I print out that soul data you can see we have here just an object with only the different keys that we want, essentially each one of the days of Mars.

  • Now, the next thing that I want to do is I want to take this data which is in this really difficult to use format, and I want to just take out the exact pieces of information that we need and save them in a new object.

  • So if we just check here, you can see as you remember, we had our temperature high and low our wind speed as well as which soul we're on.

  • So inside here 80.

  • That's our atmosphere temperature so we could get our minimum, which is mn and our maximum.

  • So let's make sure that we do that.

  • And in order to loop through all of this different data, all we need to do is say object dot entries.

  • This is going to allow us to look through an object which we can pass in our soul data object.

  • And then we just want to map this which essentially is going to convert one set of information into another set of information.

  • Make sure this says entries here and this map is going to take in the different properties from our soul.

  • Data are sold dated when we call entries is going to be an array for the first value is the key, which in our case, is for 51 the second value is everything in the objects.

  • All the A.

  • T h w s pre all of that stuff So what we can do is actually just d structure that into our soul, which is the key and our data, which is everything else.

  • And then we just want to call a function with that information and inside of here, what we want to do is return a new function, which has the information that we want.

  • So if you remember, we want our soul.

  • So we're gonna say soul, we want to get our maximum temperature will call it Max temp.

  • And that is our data 0.80 dot We open this up, we have m X for Max, So we'll say MX here.

  • We could get our men 10th which is data 0.80 dot m n for our minimum.

  • Next, we're going to need to be getting our wind speed, which comes from H.

  • W s.

  • That stands for horizontal wind speed.

  • So we can say wind speed.

  • We're gonna get data dot h W s, and we just want to get the average, which is a vey because we don't have a minimum or maximum showing.

  • We just show on average value across the entire day.

  • Next, we need to make sure we get the degrees for our wind because, as you can see, we have this little icon here which shows the direction the wind is blowing.

  • So we want to get our wind direction in degrees.

  • This is gonna be data dot w d instead of here.

  • This is our wind direction, and we have this most common section which is essentially like our average, so we can call most common.

  • And then we want to get the compass degrees that's going to give us the degrees of our wind.

  • And also we want to make sure you get this compass point so that will we have a way to tell screen readers.

  • So people that cannot see this nice icon exactly which direction the wind is going.

  • So we're gonna do that again, which is just going to be called wind direction Cardinal, which is just saying which direction it's coming from.

  • You're gonna be data dot w d dot most common.

  • And this one is the compass point just like that.

  • That's going to be this South southeast designation right here and then.

  • Lastly, I want to get the date that this happened because, as you can see here, we're printing out the actual date on Earth as well as the date on Mars.

  • So we just want to make sure we get that date.

  • We're gonna call it a date here, and we can just convert this value instead of here, which is the scroll up first UTC.

  • So we can just convert that first UTC by calling data dot first hoops first UTC and this new date is just gonna convert this string value into an actual date.

  • We can use with JavaScript and print out to the screen like this.

  • And with that, what we can do is we can actually just print out what gets returned from this so we can come in here and we can say that we would want to set this to a variable will say attempt because this is just gonna be temporary.

  • And then down here, we could just cancel that log our tent variable, and if we save, you can see Now we have just a array of objects, and each one has the exact information we want the soul, maximum temperature, wind speed, wind direction, the direction card in ality as Bose, an actual date that we can use with JavaScript.

  • So in order to use this information instead of just logging it out, let's make sure that we return this from our dot then And we also want to return our promise from get weather And then when we call and get whether it's returning a promise, so we could just say dot then and our value being returned is this array right here that we know is going to be our list of all of our different souls.

  • So as soon as we're done getting our weather, our souls are gonna be in here and again.

  • I can just log this out save, and we get the exact same Arabian printed out.

  • So now this get weather function is essentially done, and the exact data we need is coming into this dot Then portion when we call, get weather.

  • So now we have the array of all of our different souls exactly where we want it.

  • And now with that data, by default, I want our latest soul to be displayed.

  • Here is our main soul section.

  • So essentially the most recent date I want to display here and our most recent date is just going to be the last soul in our list, so we can have a variable which were just kind of call Selected Soul Index.

  • This is the one that's currently being displayed in this large overhead section because we have all these other souls down here and we want to display our over our king one up here in this large section so that selected his soul index is just our souls dot length minus one.

  • This is going to give us the very last index in our array.

  • And I also want to make this a global variable because we want to be able to access this selected soul index from anywhere in her application socials put our selected soul index up here and then just redefined that value down here.

  • So now it's a global variable available in any of our different functions.

  • So now that we have that, I want to be able to display that specific selected soul.

  • So let's create a function to do that.

  • We're gonna call this function display selected soul, and this is gonna take in the array of all of our different souls, and we could just call that here display selected soul with all of our souls.

  • And the first thing I want to get is our selected soul.

  • And this selected soul is just gonna be equal to our souls array.

  • We want to get from that selected soul index.

  • So there's value here in this variable is going to be which ever soul is currently selected or by default is going to be the very last soul.

  • And we can prove that by just logging it out.

  • If we inspect over here, you'll see that we have just an individual object, which is the very last soul in our souls of Ray.

  • So now we can take the information from this, for example, our soul number, our temperatures, wind speed, wind direction and our wind direction card in ality and put all of that information into our HT mountain.

  • But in order to do that, we need to modify our HTML so that we can select each one of these different sections to place the exact data that we want.

  • So let's go ahead and do that.

  • We're gonna open up our index dot html and inside it here we'll scroll down a little bit until we find our section for example, right here where we have our soul being displayed and in order to display a specific soul here instead of always 377.

  • What I want to do is just delete this 3 77 that shows up by default.

  • And instead I'm just going to have a span inside of here which is going to have a data attributes which is called Current Soul.

  • And now we can select this data attributes inside of our JavaScript.

  • So when we save, you can see that default value has disappeared, which is exactly what we want.

  • And now, instead of here, what we can do is we can create a selector which we can just call current soul element.

  • We're gonna set that to document that query selector we're gonna select for that exact date attributes, which is data current soul hopes soul just like that.

  • And now this current soul element we can use instead of our display selected soul, we can set the inner lips and her text to be equal to selected soul dot soul.

  • Now, when we save, you can see that number 4 57 is being printed out right onto her html.

  • So we're getting the information from the A.

  • P I.

  • And actually printing out that exact sole tore html just like this.

  • Now, the next thing to do is going to be the exact same thing, but for a different temperature sections as well as for wind speed.

  • So let's go over to our index and do that.

  • Now the first thing we need to set is our date.

  • So instead of here, we can say data current date and just delete the default date.

  • And if we save that date gives away next is going to be our temperature section.

  • I'm gonna break this onto a few different lines to make it easier to work with.

  • Just like that, in our negative 20 section.

  • We want to replace this with a SPAN again, and this span is going to have a data attributes and this one is gonna be called current temp, and this one's going to be our high temperature.

  • So we'll say current temp I and for our low temperature right here, we're gonna do essentially the exact same thing.

  • But instead of having our current temp high, we're gonna use current temp low, so we're gonna replace us with current Templo.

  • Now, the last few sections we have is our wind section.

  • So here we have our wind speed, which we're gonna need to modify.

  • So again, I'm just gonna put this on multiple lines to make it easy to work with.

  • We're gonna use the span here, and this one is gonna say data, wind speed.

  • Lastly, all we need to do is have our wind direction, which is down here.

  • So let's save data, Wind direction arrow.

  • This is that red area you see on the right hand side of the screen, which will point in which ever direction we want.

  • And as you can see, Kevin's left a note saying that there's a direction, custom prop on that which we can change in order to change which way this aero faces.

  • And then, lastly, there's this screen reader only section.

  • So this will get read out to people on a screen reader.

  • So what we want to do here is just delete the default and put in our selector, which is gonna be data, wind, direction, text.

  • And that's the south, south, east or North East number that we saved inside of our Jason.

  • We're gonna place that instead of our screen reader only section.

  • So now with that, that's all of the HTML changes we need for a top section.

  • And as you can see, all of our default values have disappeared.

  • So now it's time to jump into our JavaScript, pull out all of our selectors for that and start placing in those new values from the A P I.

  • So first place.

  • Just copy this down because we're gonna do a bunch of selectors here.

  • We're gonna do our current date element, which is going to be data current date.

  • Just like that.

  • We're gonna have another one, which is our current temp.

  • Hi.

  • I'm gonna call this current quips, Tim.

  • Hi.

  • And we're gonna do the exact same thing, but for our current temps in the low direction.

  • So copy that down.

  • Copy it down.

  • Another time for all of our wind sections.

  • So we're gonna have wind speed element and here is gonna say data, wind speed.

  • We're also going to have another one here, which is going to be what's wind direction text.

  • We can remove this element section here, and also we can remove current from these because there's only one place that we display window.

  • So we don't need to specify current because it's always gonna be the current.

  • And here we can say direction, text.

  • And then lastly, we're gonna have our aero.

  • So you say wind direction, arrow.

  • And here when Direction Arrow.

  • So that's all of our selectors for the different elements in our job script that working a customized So now, inside of our display selected soul Let's just set them to the values we pulled from the a p I.

  • So 1st 1 we have is our current date element dot inner text.

  • I'm risking a set that to our selected soul dot date.

  • Next, we're gonna have our current 10th attempt hide element, and we want to get the inner text here and select are selected soul dot max temp flips 10.

  • Well, copy that down because our temple oh is nearly identical, but this is just going to be our men temp.

  • Next, we're gonna have our wind speed element.

  • We want to get the inner text of this one as well and said that two are selected selected soul that wind speed and we're gonna do a very similar thing for our wind direction wind direction, arrow.

  • But this time, instead of actually specifying inter text, what we want to do is specify a style because we want to set that custom property so we can say style dot set property.

  • And remember, this property name is just dash dash direction and in here we can set this toe are selected soul clips selected soul dot wind direction in degrees but we need to make sure we convert this to a degree value.

  • So it's just come in here and use string interpolation.

  • We'll just put in here these selectors so that we have our value being printed out and then we could just put the e g at the end to convert this to a degree value.

  • Inside of CSS.

  • The very last thing we have is our wind direction text and this again we could just set our inter text to our selected soul dot Wind direction text Actually not text story card.

  • No, there we go.

  • And if we save, you can see a bunch of things happen immediately.

  • Our temperatures are being displayed high and low.

  • We have our wind speed being displayed, and the arrow is updated correctly and our date is being displayed.

  • But you'll notice almost none of these values are very well formatted.

  • We have a bunch of desperate places on all over different formats, and this date is absolutely massive.

  • So instead, what I want to do is actually change this so we can have nice display format for all over different values.

  • The very first thing I want to change is going to be our date.

  • Let's just create a function called display date, which is gonna take in a date and then give us a nicely displayed version of it.

  • So we'll create that function display date, which takes in a date, and all we want to do is return date dot to locale string.

  • Sorry to look outthe date string, and this function is going to take two different properties.

  • The first is the low cow you want to convert to so essentially what language you want, or you can pass an undefined and it'll determined the language based on the user's browser and what defaults they have set next.

  • What you want to do is we want to say that we want the day format of this to be in numeric format, So we just want a number day, and we want our month to be in the format of long, so we want to display out the full month name.

  • Now, if we save, you can see we get March 9th Green printed out that already on its own is looking much better now.

  • Next, let's take a look at doing a display version of our temperature so we'll say display temperature.

  • And we're just going to do that for both of our different temperatures.

  • Come down here for a minimum temperature and did the exact same thing, and we're gonna make this a little bit wider, so it's easy to see.

  • It'll just scrunch up the side of our screen.

  • Just so our JavaScript code is leader and easier to read.

  • So we're gonna have that display temperature function, which is gonna take in a temperature.

  • And all we're gonna do for now is return math dot round, and we just want around that temperature so we'll say temperature.

  • And if we say you can see immediately regretting whole numbers instead of getting decimals, which is exactly what we want, what's due a very, very somewhere thing.

  • But for our display speed, so we can say display speed, which is going to take in a speed, is just going to round our speed.

  • And the reason right now that I have these in two separate functions is because eventually we need to convert between Celsius and Fahrenheit.

  • As you can see down here, we have this toggle we need to build a convert between these different values and temperature and speed have different conversion rates, which is why they're in different functions, even though right now they do the exact same thing.

  • So you used that display speed function to get our wind speed rounded, and if we save, you can see that the top portion of our website looks really good.

  • We have all of our different displays being nicely rounded.

  • It's coming directly from the A P I, and everything looks good, but you'll notice this previous seven days section is still full of placeholder data.

  • We need to replace this with actual real world data.

  • In order to do that, let's go into our html widen this back out and scroll down to this section called previous days This is where each one of the previous day's is displayed.

  • And they all have this class previous day.

  • And what we want to do is just take one of these previous days, copy it out, and we're gonna put it down into its own section.

  • For now, fix this indentation.

  • Here we go.

  • And all we want to do is remove everything else inside of this previous days section.

  • So just don't delete all of these different days that currently exist.

  • And we're gonna set a class to this arm.

  • Sorry, Data attributes.

  • I'm gonna call this data previous souls and then this section.

  • What we're going to do is actually wrap this inside of what's called a template.

  • So we're just gonna say here, template, and this is gonna have a data attributes, which is previous soul template.

  • And the nice thing about templates is they don't actually get rendered to the page.

  • These templates are just able to be used inside a job script because we can use this selector that we put on this template in order to select this inside of our javascript.

  • So now if we save, you'll notice that this previous days section is empty, which is exactly what we want.

  • And all we need to do is modify HTML down here slightly in order to make it so that each of our sections in our HTML is able to be modified in Java script.

  • Essentially, we need to do the same thing down here that we did all the way up in this section for our current temperatures and such.

  • So we need to do this for our current soul.

  • Let's just break this out just like we did before onto multiple lines.

  • We're gonna have a span here, which is gonna have a data attributes.

  • We're just gonna say, Data Soul, We're gonna have another thing here for a date.

  • Data date.

  • Let's just remove this place Older date.

  • We're gonna do a similar thing for our temperature.

  • So let's come down here.

  • But these under multiple lines, just so it's easier to work with.

  • And we can come in here with this fan with the data attributes.

  • We're gonna call this one data temp high.

  • We're going to do almost exactly the same thing for Arlo section.

  • So let's just break these under multiple different lines and we want to just copy over this selector, but we're gonna call this temp low and then very finally all the way down here.

  • We need to have a data select button for this more info.

  • Because when we click more info, we want to change our current soul up in this top section to which ever one is currently selected by this more info button.

  • So now we can save and you'll still notice down here we have nothing in our previous seven days.

  • That's okay for now, because our actual HTML is going to be generated by our JavaScript.

  • So the first thing we need to do is create a function which is going to allow us to display those previous souls.

  • It's protocol, it display previous souls, and it will again take in the list of all of our souls, just like we've done up here.

  • And all we need to do is display every single one of those souls.

  • But we first need to get a selector for all of those different attributes that we just specified in our HTML here so we can scroll up.

  • I'll create a new section for these, just kind of to make it obvious that this is our first section of attributes appear than the second section is for our previous attributes.

  • And luckily for us, there's only two different attributes we need to select.

  • We need to get our previous soul template.

  • This is that template tag that we created, which has all of our different HTML.

  • And this is just document dot clary selector.

  • And we have a date attribute on this cold data previous soul template, and we need to do essentially the exact same thing.

  • But this is going to be pro previous soul container, and we'll just be previous soul container.

  • And then here we have data previous souls, as our data attributes selector.

  • And this is just that a list right here where we're gonna put all of our different souls from this template.

  • So now that we have those selected, we can save come all the way down here and then inside of this function, the first thing I want to do is take our container and I just want to clear it out.

  • We're gonna set the inner HTML to nothing.

  • This will essentially remove everything inside of that container.

  • And if you save and look over here, you can see it's empty because by default it's going to be empty.

  • But if we put some content inside of here, let's just put random content like that.

  • You'll see that this content is here.

  • But as soon as we call this display, previous souls function just like this pass in our souls, you'll see that that content is now gone.

  • So essentially everything inside of here is being deleted by our JavaScript.

  • This is really nice, because every time we call this, we want to start with a fresh, empty container.

  • Now, the next thing to do is to loop through each one of our souls.

  • So we can say soul for each, and this is going to have our soul data as well as the index of the element inside of our rate.

  • So we have our actual array value and then the index of the array itself.

  • And inside here, we're gonna create a new object called a soul container.

  • And this is going to be from our template.

  • So we can say previous soul template and all we want to do is get the content inside of here clips that content and What we want to do is call clone node and pass in.

  • True.

  • Essentially, this is going to clone all of this HTML, including all of the different nesting.

  • And it's gonna put that into this soul container variable that we just created.

  • Now with that soul container we can do Cleary selectors in order to select all of these different attributes for example, data, soul data date and set the values we want.

  • So let's just do data soul first, and all I want to do is set the inner text of this to be equal to our soul data dot soul.

  • And let's just look at what we have so far.

  • When I save and I opened this bottom section up, you'll see that nothing is being generated.

  • And that's because I first need to take our previous soul container.

  • And when I append a child, which is going to be our soul container and just make sure we do that inside of our loop.

  • Now, if I open this section up, you can see we have a bunch of different souls.

  • We have 4 51 4 52 34567 Essentially all of our soul values are being displayed in this previous soul section.

  • The reason that looks so ugly right now is my screen size is just super small.

  • It's just to make the job a script easier to work with.

  • But if I expand this out, you can see it looks much better and much easier to reason with.

  • So now that we know that everything is working, what's do all of the rest of our data, we next need to select our soul container.

  • We need to get our query selector, and again, we're gonna select our date this time.

  • So what Use data date and we want to set the inner text.

  • And we already have that display date function so we can use soul data dot date and that displayed a function.

  • And now down here, we also have the date of our section being printed out, which looks really good.

  • Next let's do our temperatures so we can have our soul container query selector, and we're going to get art data temp high and in here will set our inner text equal to our display clips temperature and we want to pass in our soul data that Max temp.

  • What did the exact same thing for a temp low.

  • And instead of Max, we want to get our minimum temperature.

  • Now, if we look down here, you can see are high and our low temperature are being set for each one of our different sections, which is great.

  • The last thing that we have to do is our select more button.

  • We need to make it select our current soul.

  • So in order to do that really easily, we can just select that button.

  • We already put a data selector on it so we can say data select button.

  • And would you want to add an event listener to this and every time that we click this button, we want to run a specific function.

  • And in this function, we want to take that global variable, which is our selected soul index, and set it to the current index that we're on in our race.

  • And then we also want to call a function called display Selected soul, which is just this function, appear to display the individual soul we have currently selected.

  • We pass it in all over different souls.

  • And now if I change this period here to a comma, and I save and I come down here and I slept, for example, for 53 you can see all of our data in the top section, heads change to 4 53 Change for 55.

  • You can see this is updated for 4 55 and so on.

  • Each one that I select is going to update the data in the top section of our page, which is really, really good news.

  • Most of our application is actually complete at this point.

  • But, you know, there's one big problem is this toggle doesn't actually do anything.

  • And even when we click the middle, it doesn't actually slide left to right.

  • So first, let's handle this situation of clicking on the middle of this taco and making it actually ta go left to right.

  • So instead of her HTM Oh, let's find that taco.

  • Here it is.

  • And we have our unit ta go class.

  • What's at a data attributes called unit ta.

  • Go onto it so we can select this inside of our JavaScript.

  • So we can say that our unit toggle is equal to document dot query selector.

  • We want to get that data unit toggle.

  • And now, whenever we tuggle this, we can call certain functions.

  • So inside of our get weather was just say unit Tuggle And what we want to do is add an event listener and we ever we click this function or click this button.

  • I'm sorry.

  • We want to run this specific function and now inside of this function, all we want to do is change our toggle from the left to the right And in order to do that, we have two different radio buttons with these different inputs.

  • One has an idea of f A.

  • H one has an idea C e l.

  • And all we want to do is toggle which one is checked.

  • So to do that, we need to first to get those different elements So we can say constant metric radio is equal to document dot get element by i d.

  • And we know that this one is called C E l organised the exact same thing for our imperial radio.

  • So Imperial radio, this one is going to be called F a H just like that.

  • And now with these different metric and imperial radio buttons, we can come inside of the selector and we can say that our metric units is going to be equal to our metric radio dot checked so far, Metric radio is currently checked.

  • We just want it.

  • Uncheck it.

  • So we're gonna change our new metric units to the opposite of our metric radio dot checked.

  • Then we're going to set our metric radio dot checked equal to those metric units and would be the exact same thing with our imperial radio dot check.

  • We're going to set this to the opposite because obviously this is going to be lips right here.

  • Units is going to be unchecked whenever we want our metric units and it'll be checked whenever we do not want our metric units.

  • Now, if I save and click this middle section, you'll see our toggle properly goes between the two different values, which is exactly what we want.

  • And we can click the FNC and that also tacos are units.

  • But obviously we have one big problem.

  • When I have this set to Fahrenheit, everything is still in metric units.

  • We see Celsius, we see kilometers per hour, so we need to modify that.

  • So let's create a function to do that.

  • This function is simply just gonna be called.

  • We scroll down here, little ways function update units.

  • And what this function is going to do is it's going to get all of our different Celsius units that are on our screen as well as all of our different kilometer per hour units on the screen and change them two miles per hour or 2 F, depending on which one it is selected.

  • So in order to do that, we need to go to our HTML and find all of the places we use thes units.

  • So right here, for example, we have this sea icon, so I want to change this here to be inside of a span which has a data attributes, which is going to be called temperature Unit.

  • Let's just actually call that temp unit to make it easier to work with, and we'll close off that span just like that.

  • You did the exact same thing with this associates unit for a kilometers per hour.

  • We want this to be a speed unit instead.

  • And if we scroll down here even further, we're gonna have our temperature units here, So we want a temp unit and our temp unit.

  • So now when we say if you see all of our units are gone, which is OK for now, because what we can do is inside this update units function.

  • We can actually set our specific units.

  • So let's go ahead and do that.

  • We can say constant speed units is going to be equal to document that query selector.

  • We want to get every single one of them, and we can just saying here, data speed unit, which is that selector we just created.

  • Let's do the same thing for a different temp units.

  • This'll just say 10 peered and all we want to do is Luke through each one of these.

  • So say speed units 10.4 each and for each one of these units we want to do is call unit that inner text.

  • And we're gonna set that to a specific value which has come in here and do this on a new line to make it easier to read.

  • And all we want to do is check.

  • We're gonna use a function called is metric.

  • This is going to tell us whether or not we have our Celsius box checked or not.

  • And if his metric is true, then what I want to do is just return here kilometers per hour.

  • Otherwise, if it's false, I want to return miles per hour.

  • What's copy this and did the exact same thing for a temp units.

  • But instead we're gonna have a big C for Celsius and a big F for Fahrenheit.

  • And then let's create that function, which is just called his metric.

  • And this function is really simple.

  • We're just gonna return.

  • Here are metric radio dot checked and we can actually use This is metric function all the way up here inside of art click event.

  • So we can just come in here and use is metric instead.

  • And now we can call that update units function.

  • And if we say you can see all of our unit icons as well as over here are properly set.

  • And when we talk about the Fahrenheit, you see that nothing actually happens.

  • And that's because if we scroll over here little ways, we're not actually calling this function inside of this unit toggle.

  • So she's come down here and say, update the units just like that.

  • And now, when I click this.

  • You see it Talk was between Fahrenheit and miles per hour and Celsius and kilometers per hour.

  • But there's a slight problem and that is when I click Fahrenheit or associates directly.

  • It doesn't actually toggle, and that's because this is linked specifically bicycle appear to these individual inputs.

  • So we need to create event listeners as well.

  • On our metric radio.

  • We're gonna add an event listener here for a change.

  • And what this event listener is going to do is just very simply, call this update units function.

  • We're gonna do the exact same thing for our Fahrenheit.

  • So we can say Oops, I'm sorry.

  • Imperial radio dot ad event Listener for change on issues going to very simply, call the exact same function, which is update units.

  • Now when we click on F, it's going to properly change.

  • The sea will probably change and the middle sweater also changes.

  • But of course there is another problem, and that is that we're not actually changing the values inside of art thing because negative nine Celsius is obviously not the same as negative nine Fair Knight.

  • But luckily, this is actually really easy to fix.

  • If you remember all the way down here?

  • We have our display temperature and speed values.

  • So now we can just do our conversion inside of these functions.

  • So we already know by default all of our values for temperature are going to be in Celsius.

  • So let's just create a temporary variable which will call return 10th and we're going to set it to our temperature.

  • And then I just want to have a really simple if check here.

  • Essentially, if we're not metric units.

  • So we'll say if it's not metric, then what we want to do is just inside of here.

  • We want to take our return temp and we want to set it equal to our temperature.

  • Plus I'm sorry minus 32 then we're gonna multiply that by five divided by night.

  • This is just the conversion between Celsius and Fahrenheit.

  • So now we can use that return temp down here inside of our rounding.

  • And if we save and we make sure that we re render our selected soul and previous souls every time we update our units.

  • So it's copy this code into here and all these other different sections so copied into here and we'll copy it Slips right into here as well.

  • Now, if we save entitled a Fahrenheit, you can see our temperature properly updates.

  • But we need to make sure we work on our wind speed next.

  • So in order to do that, we can do a very similar thing.

  • We can create a temporary variable called return speed, set it to our speed, and then we could do that.

  • If check by saying if it is not metric, so is metric.

  • Then what we can do is take our return speed and we can convert this so we could just say our speed divided by 1.609 This will convert us two miles per hour and then we can just round that return speed.

  • So now when we talk with Fahrenheit, we get our speed set in miles per hour and you can see down here these temperatures also all of date, which is exactly what we want.

  • And we can still change between them.

  • So everything in our app is working as expected, and that's all it takes to create this Mars weather app.

  • If you enjoyed this, make sure that you check out Kevin's portion of the video, which will be linked over here and subscribe to my channel as well, is Kevin's from more videos just like this.

  • Thank you very much for watching and have a good day.

the weather.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

火星の天気アプリの作り方 (How To Build A Mars Weather App)

  • 14 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語