JSFiddle - React, Tailwind, and code Playground

by manoj jasti

JavaScript

// JS assignment for students


// #1
var Calculator = function (){};
Calculator.prototype = {
add: function(a,b){return a+b;}, 
subtract : function(a,b) {return a-b;},
multiply: function(a,b){return a*b;}, 
divide:function(a,b){return parseInt(a/b);}
};


// to Extend class 
function extend(child, parent)
{
child.prototype = new parent(); // inherit all the methods and props from this parent prototype to child
//child.prototype = object.create(parent.prototype); // other way of setting prototype of child function

// setting constructor back to child
child.prototype.constructor = child;
}


var calcTest = new Calculator();
console.log(calcTest.add(2,3));
console.log(calcTest.subtract(2,3));
console.log(calcTest.multiply(2,3));
console.log(calcTest.divide(2,3));
console.log(calcTest.divide(2,0));

//#2

var ScientificCalculator = function(){
this.sin = function(value){ return Math.sin(value);};
this.cos = function(value){ return Math.cos(value);};
this.tan = function(value){ return Math.tan(value);};
this.log = function(value){ return Math.log(value);};
}

extend(ScientificCalculator,Calculator);

var sciTest = new ScientificCalculator();

console.log(sciTest.sin( Math.PI / 2 ));
console.log(sciTest.cos( Math.PI ));
console.log(sciTest.tan( 0 ));
console.log(sciTest.log( 1 ));
console.log(new ScientificCalculator()  instanceof Calculator);


//#3
var withExponents = function(){
this.pow = function(a,b) {return Math.pow(a,b);}
this.multiplyExp = function(a,b) {return this.pow(a[0],a[1]) * this.pow(b[0],b[1]);
};
this.divideExp = function(a,b) {return this.pow(a[0],a[1])/ this.pow(b[0],b[1])};
}


var calc = new Calculator();
withExponents.call(calc);
console.log(calc.pow(2,3));
console.log(calc.multiplyExp([2,3],[2,4]));
console.log(calc.divideExp( [ 2, 3 ], [ 2, 5 ] ));

///#4

var delayCalc = new Calculator();
   function resolve(a){ 
   console.log( a[0][a[1]].apply(a[0], a[2]));
   }
   
function reject(){
    console.log("rejected")
    }
var delay = function...