JSFiddle - React, Tailwind, and code Playground

by Abid Akhtar

JavaScript

Turtle.prototype.type = "turtle";
Lion.prototype.type = "lion";
Dolphin.prototype.type = "dolphin";

var yoyo = new Turtle("Yoyo");
var simba = new Lion("Simba");
var dolphy = new Dolphin("Dolphy");

alert(yoyo.walk(10));
alert(yoyo.swim(30));   // turtles are faster in the water
alert(simba.walk(20));
alert(dolphy.swim(20));

if (isOceanAnimal(yoyo)) alert(yoyo.name + " is an ocean animal.");
else alert(yoyo.name + " is not an ocean animal.");

if (isOceanAnimal(simba)) alert(simba.name + " is an ocean animal.");
else alert(simba.name + " is not an ocean animal.");

if (isOceanAnimal(dolphy)) alert(dolphy.name + " is an ocean animal.");
else alert(dolphy.name + " is not an ocean animal.");

if (isLandAnimal(yoyo)) alert(yoyo.name + " is a land animal.");
else alert(yoyo.name + " is not a  land animal.");

if (isLandAnimal(simba)) alert(simba.name + " is a  land animal.");
else alert(simba.name + " is not a  land animal.");

if (isLandAnimal(dolphy)) alert(dolphy.name + " is a  land animal.");
else alert(dolphy.name + " is not a  land animal.");

function isOceanAnimal(object) {
    if (typeof object !== "object") return false;
    if (typeof object.swim !== "function") return false;
    return true;
}

function isLandAnimal(object) {
    if (typeof object !== "object") return false;
    if (typeof object.walk !== "function") return false;
    return true;
}

function Turtle(name) {
    this.name = name;
    LandAnimal.call(this);
    OceanAnimal.call(this);
}

function Lion(name) {
    this.name = name;
    LandAnimal.call(this);
}

function Dolphin(name) {
    this.name = name;
    OceanAnimal.call(this);
}

function OceanAnimal() {
    this.swim = function (n) {
        return "I am " + this.name + ", the " + this.type +
               ", and I just swam " + n + " meters.";
    };
}

function LandAnimal() {
    this.walk = function (n) {
        return "I am " + this.name + ", the " + this.type +
               ", and I just walked " + n + " meters.";
    };
}