d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Suck At This, Someone Help (javascript)
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 15 2013 06:19pm
Simple homework assignment but I apparently missed something

HTML:

Code
<!DOCTYPE html>

<html>

<head>

   <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
   <title>Car</title>

</head>


<body>
   <script type="text/javascript" src="obj.js"></script>
</body>

</html>


JS:


Code
function car(make, model) {
   this.make=make;
   this.model=model;
   this.running=false;
   this.tire1=25;
   this.tire2=25;
   this.tire3=25;
   this.tire3=25;

   function start() {
       this.running=true;
   }

   function stop() {
       this.running=false;
   }

   function checkEngine() {
       if (this.running=true) {
           document.write("Car is running!");
       }
       else {
           document.write("Car is currently off.");
       }
   }

   function pressure() {
       document.write("<h3>Tire pressures: </h3>"this.tire1 + this.tire2 + this.tire3 + this.tire4);
   }

   function inflate() {
       this.tire1=30;
       this.tire2=30;
       this.tire3=30;
       this.tire4=30;
   }

   function deflate() {
       this.tire1-=1;
       this.tire2-=1;
       this.tire3-=1;
       this.tire4-=1;
   }
}

var scion=new car("Scion", "xB");
scion.checkEngine();
scion.start();
scion.checkEngine();
scion.stop();
scion.checkEngine();
scion.pressure();
scion.inflate();
scion.pressure();
scion.deflate();
scion.pressure();


Assignment is to make an object, give it properties and methods, then show that they all work on a webpage by utilizing each method

It won't run (the script). I had another small assignment from earlier that was supposed to prompt the user twice and it wouldn't run either.

I get a blank page.

What am I doing wrong?

extra notes:

in the chrome debugger, my .js was never listed as a source? That seems like a red flag
Professor ONLY wants external javascript, no inline

This post was edited by Eep on Sep 15 2013 06:33pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 15 2013 06:43pm
/edit: nevermind. thought you couldn't nest functions but it looks like you can.

have you tried the normal trouble shooting strategies? eg get rid of everything in your file and just put an alert to see if it runs. if it does, slowly add more code until it breaks. if it doesn't run, then you set it up incorrectly, so you could try to define everything in the same file just to make sure your javascript itself is valid.

This post was edited by carteblanche on Sep 15 2013 06:50pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 15 2013 06:45pm
It seems weird as hell to me as well, but the initial 'function' was supposed to be a constructor iirc.

like this example:
http://www.w3schools.com/js/tryit.asp?filename=tryjscreateobject3

Quote (carteblanche @ Sep 15 2013 07:43pm)
/edit: nevermind. thought you couldn't nest functions but it looks like you can.

have you tried the normal trouble shooting strategies? eg get rid of everything in your file and just put an alert to see if it runs. if it does, slowly add more code until it breaks. if it doesn't run, then you set it up incorrectly.



yeah I went thru it kind of hastily...guess I underestimated it

This post was edited by Eep on Sep 15 2013 06:47pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 15 2013 08:55pm
Solved my issues, I am not used to this style of OOP that javascript does :P

gotta work work on it

thanks carte for reminding me to slow down...like you said, I found out where every error was.

This post was edited by Eep on Sep 15 2013 08:56pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll