Created
March 3, 2015 22:34
-
-
Save hackingbeauty/84a2e401cca17fefb99b to your computer and use it in GitHub Desktop.
Interview test I had
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| function Calculator(){ | |
| } | |
| Calculator.prototype.add = function( int1, int2 ){ | |
| return int1 + int2; | |
| }; | |
| Calculator.prototype.subtract = function( int1, int2 ){ | |
| return int1 - int2; | |
| }; | |
| Calculator.prototype.multiply = function( int1, int2 ){ | |
| return int1 * int2; | |
| }; | |
| Calculator.prototype.divide = function( int1, int2 ){ | |
| if( int2 === 0 ){ | |
| return NaN; | |
| } else { | |
| return int1/int2; | |
| } | |
| }; | |
| var calculator = new Calculator(); | |
| console.log( calculator.add(1,2) ); | |
| console.log( calculator.subtract(9,2) ); | |
| console.log( calculator.multiply(4,3) ); | |
| console.log( calculator.divide(10,2) ); | |
| console.log( calculator.divide(5,0) ); | |
| function ScientificCalculator(){ | |
| } | |
| ScientificCalculator.prototype = Object.create(Calculator.prototype); | |
| ScientificCalculator.prototype.constructor = ScientificCalculator; | |
| ScientificCalculator.prototype.sin = function( int ){ | |
| return Math.sin(int); | |
| }; | |
| ScientificCalculator.prototype.cos = function( int ){ | |
| return Math.cos(int); | |
| }; | |
| ScientificCalculator.prototype.tan = function( int ){ | |
| return Math.tan(int); | |
| }; | |
| ScientificCalculator.prototype.log = function( int ){ | |
| return Math.log(int); | |
| }; | |
| var calculator = new ScientificCalculator(); | |
| console.log(calculator instanceof Calculator); | |
| console.log(calculator instanceof ScientificCalculator); | |
| console.log(calculator.sin( Math.PI / 2 )); | |
| console.log(calculator.cos( Math.PI) ); | |
| console.log(calculator.tan( 0 ) ); | |
| console.log(calculator.log( 1 )); | |
| var withExponents = function(){ | |
| this.pow = function( int1, int2 ){ | |
| return Math.pow( int1, int2 ); | |
| }; | |
| this.multiplyExp = function( intArr1, intArr2 ){ | |
| return Math.pow( intArr1[0], intArr1[1] ) * Math.pow( intArr2[0], intArr2[1] ); | |
| }; | |
| this.divideExp = function( intArr1, intArr2 ){ | |
| return Math.pow( intArr1[0], intArr1[1] ) / Math.pow( intArr2[0], intArr2[1] ); | |
| }; | |
| }; | |
| var calculator = new Calculator(); | |
| withExponents.call( calculator ); | |
| console.log( calculator.pow(2,3) ); | |
| console.log( calculator.multiplyExp( [2,3], [2,4] ) ); | |
| console.log( calculator.divideExp( [2,3], [2,5] ) ); | |
| function delay( delayNum, obj, method, args ){ | |
| return new Promise(function(resolve, reject) { | |
| if (typeof obj[method] === 'function'){ | |
| setTimeout( | |
| function() { | |
| // We fulfill the promise ! | |
| resolve(console.log('obj', obj[method](args[0], args[1]))); | |
| }, delayNum); | |
| } else { | |
| reject(); | |
| } | |
| }); | |
| } | |
| var calculator = new Calculator(); | |
| console.log(delay( 1400, calculator, 'add', [10,5])); | |
| console.log(delay( 1400, calculator, 'subtract', [9,5])); | |
| console.log(delay( 1400, calculator, 'sqrt', [2,2])); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment