JSFiddle - React, Tailwind, and code Playground
HTML
<strong>Output</strong>
<div id="output">
</div>
<hr />
<strong>Expected output</strong>
<div>
My dog Spot says: woof<br />
My cat Mittens says: meow<br />
My worm Squiggles says: nothing
</div>
JavaScript
try {
// create animal class
var animal = function(name) {
this.name = name;
};
animal.prototype.getSound = function() {
return this.sound === undefined ? 'nothing' : this.sound;
};
// create animal type classes
var dog = new animal("dog");
dog.prototype.sound = "woof";
var cat = new animal("cat");
dog.prototype.sound = "meow";
var worm = new animal("worm");
// create specific animals
var spot = new dog();
var mittens = new cat();
var squiggles = new worm();
// echo output
document.getElementById("output").innerHTML = "My dog Spot says: " + spot.getSound() + "<br />"
"My cat Mittens says: " + mittens.getSound() + "<br />"
"My worm Squiggles says: " + squiggles.getSound();
}
catch(e)
{
document.getElementById("output").innerHTML = e;
}