Question
Part1.Start with this Squirrel program. Modify so that all Squirrels have a jump() method. Add all of your Squirrels to a list. Iterate over the
Part1.Start with this Squirrel program.
Modify so that all Squirrels have a jump() method.
Add all of your Squirrels to a list. Iterate over the list using a for loop. Print each squirrel's name, and called the .jump() method for each squirrel.
//Acts as our class definition - define a constructor
var Squirrel = function(n) {
this.name = n;
this.nuts = 0;
};
//Add a method to the Squirrel class
Squirrel.prototype.sayHello = function() {
console.log("Hello!");
};
//Add another method
Squirrel.prototype.faveFood = function(){
console.log(this.name + " likes nuts");
};
//And another method
Squirrel.prototype.addNutsToStore = function(newNuts) {
this.nuts += newNuts;
};
var fluffy = new Squirrel("Fluffy");
//Call some squirrel methods for fluffy
fluffy.sayHello();
fluffy.addNutsToStore(10);
fluffy.faveFood();
fluffy.addNutsToStore(15);
console.log(fluffy.nuts);
var squeaky = new Squirrel("Squeaky");
squeaky.faveFood();
squeaky.sayHello();
//Can you call addNutsToStore for this Squirrel?
//Adding properties to one Squirrel only
squeaky.tree = "Oak Tree";
console.log("Squeaky's tree is : "+ squeaky.tree); // "Oak Tree"
//Fluffy doesn't have a tree
console.log("Fluffy's tree is : "+ fluffy.tree); // "undefined"
//Add a jump method only for fluffy
fluffy.jump = function() {
console.log(this.name + " is jumping!");
};
fluffy.jump(); //This works
// squeaky.jump(); //Calling this results in an error - squeaky doesn't have a jump method
Part2.
Use the setInterval method to draw a circle moving across your canvas. You'll need to clear the canvas with clearRect(), and draw the circle in a new position every interval. (Here's an example of setInterval updating a canvas
Part3.
Create a page for a very simple drawing program. When you move the mouse over the canvas, mousemove events are generated. For every mousemove event, draw a small circle under the mouse pointer. So, the user will be able to use the mouse to draw.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started