Now in part two, we're going to take a look at how to solve exercise 10 using a constructor function. So let me jump to sublime. I'm just going to go ahead and comment out what we've previously done here. And below that is where I will do the constructor. So let me give us some lines so that you're able to see what I'm doing there. So to start with, I need to set up a function and this will be the function that I will use as a constructor.
So use the function keyword, I'm going to call it game character. And I'm going to make it possible to pass in speed, strength and hit points. All right now, this is where it's a little bit different because if one of these parameters is not passed in, I don't want to set it on the object, because we want that particular property to not be available so that it then uses the property from the prototype. So I'm just going to use if conditionals to check to see if it was provided. So if speed using truthy and falsy if the values provided it will be true. If the value is not provided, it's either null or undefined and that evaluates to false.
So, if it is provided, I can go ahead and set this speed equal to speed. And I can do that exact same thing with strength and hit points. So if strength this dot strength equals to strength and then finally, if hitpoints This hit points equal points. All right. So that will set things up for us when we call our constructor function. Now we want to establish some things on the prototype.
And so the way we do that is game character, we want to get the prototype of this function. game character dot prototype dot speed equals six is as the default value. Now we do the same thing with strength. And that's equal to eight. And then we do the same thing with hit points and we wanted that equal to 150. Now we've got to add the damage per second function or method.
So whoops, prototype first, dot damage per second. Let's set that equal to a function. And then the function will return this dot speed times this dot strength semicolon at the end of that statement. All right, so that's set up, we have our constructor function, we have things assigned to the prototype. So now we can go ahead and create a sample character just to test it out. So we're going to use character one again, and set that equal to new game character.
And we're going to pass in a nine for speed, no for strength, and then 205 for hit points, okay. So the know will indicate that we're not passing anything in for strength. And then of course, you want to console log char one dot damage per second, just to see what that equals to make sure we did it right. Save that. And let's take a look at our results. Open the console, and we got a 72 for that one as well.
So it looks like that one is working. This was done with the constructor function, getting the same results and it's being handled the same way just different syntax. Okay, proceed to part three of exercise 10 to see how this would be done with a class definition.