var Penguin = function (name) { //Penguin ctor
this.name = name;
this.awake = true;
this.asleep = false;
}
// ----- setters -----
Penguin.prototype.setName = function setName(name) {
if (typeof name === 'string'){
var oldName = this.name;
this.name = name;
console.log("You changed " + oldName + "'s name to " + name + ". I liked it better before.")
}
else
console.log("Please enter a string for your penguin's name.")
}
Penguin.prototype.setAge = function setAge(age){
this.age = age;
console.log("You changed " + this.name + "'s age to " + age + ".");
}
Penguin.prototype.setColor = function setColor(color){
this.color = color;
console.log("You changed " + this.name + "'s color to " + color + ".");
}
Penguin.prototype.setSex = function setSex(sex){
sex = sex.toLowerCase();
if (sex === "male" || sex === "female"){
this.sex = sex;
console.log("You changed " + this.name + "'s sex to " + sex + ".");
}
else
console.log("Sorry, " + "'" + sex + "'" + " is not a valid answer. Please answer with male or female.");
}
Penguin.prototype.setHeight = function setHeight(height){
this.height = height;
console.log("You changed " + this.name + "'s height to " + height + " inches.");
}
Penguin.prototype.setWeight = function setWeight(weight){
this.weight = weight;
console.log("You changed " + this.name + "'s weight to " + weight + " lbs.");
}
// ----- getters -----
Penguin.prototype.getName = function getName(){
return this.name;
}
Penguin.prototype.getAge = function getAge(){
if (this.age)
return this.age;
else
console.log("You haven't set " + this.name + "'s age yet. (Hint: Use a setter)");
}
Penguin.prototype.getColor = function getColor(){
if (this.color)
return this.color;
else
console.log("You haven't set " + this.name + "'s color yet. (Hint: Use a setter)");
}
Penguin.prototype.getSex = function getSex(){
if (this.sex)
return this.sex;
else
console.log("You haven't set " + this.name + "'s sex yet. (Hint: Use a setter)");
}
Penguin.prototype.getHeight = function getHeight(){
if (this.height)
return this.height;
else
console.log("You haven't set " + this.name + "'s height yet. (Hint: Use a setter)");
}
Penguin.prototype.getWeight = function getWeight(){
if (this.weight)
return this.weight;
else
console.log("You haven't set " + this.name + "'s weight yet. (Hint: Use a setter)");
}
// ----- methods -----
Penguin.prototype.swim = function swim() {
console.log(this.name + " loves the water.");
}
Penguin.prototype.eat = function eat() {
console.log(this.name + " ate a fish! Great for " + this.name + ", not so great for the fish.");
}
Penguin.prototype.walk = function walk() {
console.log("Walking is something that " + this.name + " does a lot.");
}
Penguin.prototype.slap = function slap() {
console.log("That was one hell of a slap, " + this.name + "!");
}
Penguin.prototype.sleep = function sleep() {
if (this.awake === true){
this.awake = false;
this.asleep = true;
console.log(this.name + " fell asleep. Sweet dreams.");
}
else
console.log(this.name + " is already asleep.");
}
Penguin.prototype.wake = function wake() {
if (this.awake === false){
this.awake = true;
this.asleep = false;
console.log("Good morning, " + this.name + ".");
}
else
console.log(this.name + " is already awake.");
}
var penguin1 = new Penguin("Louie");
var penguin2 = new Penguin("Sally");
penguin1.sleep();
penguin1.wake();
penguin2.slap();
penguin2.setAge(4);
penguin2.getAge();
penguin1.setSex("Male");
penguin2.setSex("Female");
penguin2.swim();
Jump into the console to see the results. Also, try using some of the methods that I didn't include.
Also, thank you to Google for providing Prettify. I hated the black and white code.