5 Replies - 1067 Views - Last Post: 19 May 2010 - 12:17 AM

#1 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Javascript question

Posted 18 May 2010 - 10:50 AM

hi

7. Create javascript so that the following methods produce the output listed next it.


circle = new Circle();

console.log(circle.get_area());  // 3.141592653589793

circle.set_radius(10);

console.log(circle.get_area());  // 314.1592653589793

console.log(circle);             // the radius of my circle is 10 and it's area is 314.1592653589793





Not sure about the wording of circle.get_area() == 3.14159.. i.e. PI? I mean, surely get_area() means calculate the area not the mathematical constant PI? (maybe it's a trick question)

Not sure about the console.log() method, although i believe it to be some sort of debbuger function.

I need a simple object/class with some properties/methods, namely radius, get_area();

There are quiet a few examples online, but i cant find a simple one that fits my needs or answers my question.


I'm starting with this,

<script type="text/javascript">
function circle(radius) {

	this.radius = radius;
	this.area = function() {alert(this.radius * this.radius * Math.PI)}


}
</script>



i need to instantiate it and call it within an html documnent. Help appreciated.


:helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript question

#2 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Re: Javascript question

Posted 18 May 2010 - 02:20 PM

I hope your not asking anyone to do your homework! :bigsmile:

Try this http://tinyurl.com/33uzoxq (Creating custom objects in Javascript)

Hope it helps.

This post has been edited by JohnWaters: 18 May 2010 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: Javascript question

Posted 18 May 2010 - 02:37 PM

View PostJohnWaters, on 18 May 2010 - 01:20 PM, said:

I hope your not asking anyone to do your homework! :bigsmile:

Try this http://tinyurl.com/33uzoxq (Creating custom objects in Javascript)

Hope it helps.


Ive hit that page before John, thanks. Me thinks people are a just a little paranoid about 'homework' it seems. And all the effort i put in as well, whehu... :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#4 mr.coder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 31-January 10

Re: Javascript question

Posted 18 May 2010 - 09:19 PM

Here is your code, worked out:


<script type="text/javascript">

function Circle() 
{     
	this.pi=Math.PI; 
	this.radius=1;
}    

Circle.prototype=
{
	get_area: function(){return this.radius*this.radius*this.pi;},
	set_radius: function(radius){return this.radius=radius;}
};
 
Circle.prototype.toString=function(){return "The radius of my circle is "+circle.radius+" and it's area is "+this.get_area();}  

circle = new Circle();

console.log(circle.get_area());  // 3.141592653589793

circle.set_radius(10);

console.log(circle.get_area());  // 314.1592653589793

console.log(circle);             // the radius of my circle is 10 and it's area is 314.1592653589793

</script>




If you are unsure on how to use the console.log(), use the alert() function to get the answer!

Hope it helps!
Was This Post Helpful? 1
  • +
  • -

#5 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: Javascript question

Posted 19 May 2010 - 12:08 AM

View Postmr.coder, on 18 May 2010 - 08:19 PM, said:

If you are unsure on how to use the console.log(), use the alert() function to get the answer!

Hope it helps!


Thankyou mr.coder. At last, someone who isn't afraid to help out. :rolleyes2:
Was This Post Helpful? -1
  • +
  • -

#6 mr.coder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 31-January 10

Re: Javascript question

Posted 19 May 2010 - 12:17 AM

View Post1cookie, on 19 May 2010 - 11:38 AM, said:

Thankyou mr.coder. At last, someone who isn't afraid to help out. :rolleyes2:


Anytime, Good that you found the code useful! :balloon:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1