Last active
January 23, 2022 16:46
-
-
Save Matsukii/d40521ad8033daca7467d8177afd7fef to your computer and use it in GitHub Desktop.
Number things OOP'ed
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
| /** | |
| * @description A class to check numbers | |
| * LICENSE: MIT | |
| new NumChecks(Number(prompt("give me a number"))).isEqual(Number(prompt("Now give me another number"))) ? alert("Equal") : alert("Not equal"); | |
| */ | |
| class NumChecks{ | |
| operations = { | |
| "+": (a, b) => a + b, | |
| "-": (a, b) => a - b, | |
| "/": (a, b) => a / b, | |
| "*": (a, b) => a * b, | |
| "%": (a, b) => a % b | |
| } | |
| supressWarnings = false | |
| constructor(number, supressWarnings=false){ | |
| if(typeof number !== "number") return new Error("number must be a number"); | |
| this.number = number; | |
| this.supressWarnings = supressWarnings; | |
| } | |
| /** | |
| * @description Checks if the stored number is bigger | |
| * than the given argument | |
| * @param {Number} number2 The number to be compared | |
| * @returns {Boolean} true if bigger, false if smaller | |
| * | |
| * @example new NumChecks(10).isBiggerThan(5) -> true | |
| * @example new NumChecks(5).isBiggerThan(10) -> false | |
| */ | |
| isBiggerThan(number2){ | |
| if(typeof number2 !== "number") return new Error("number2 must be a number"); | |
| if(this.number > number2){ | |
| return true | |
| } | |
| return false | |
| } | |
| /** | |
| * @description Checks if the stored number is smaller | |
| * than the given argument | |
| * @param {Number} number2 The number to be compared | |
| * @returns {Boolean} true if smaller, false if bigger | |
| * | |
| * @example new NumChecks(10).isBiggerThan(5) -> true | |
| * @example new NumChecks(5).isBiggerThan(10) -> false | |
| */ | |
| isSmallerThan(number2){ | |
| if(typeof number2 !== "number") return new Error("number2 must be a number"); | |
| if(this.number < number2){ | |
| return true | |
| } | |
| return false | |
| } | |
| /** | |
| * @description Compare the stored number with a the | |
| * the argument, checking if they are equal | |
| * @param {Number} number2 The number to be compared | |
| * @returns {Boolean} true if equal, false if different | |
| * | |
| * @example new NumChecks(10).isEqual(10) -> true | |
| * @example new NumChecks(5).isEqual(6) -> false | |
| */ | |
| isEqual(number2){ | |
| if(typeof number2 !== "number") return new Error("number2 must be a number"); | |
| if(this.number == number2){ | |
| return true | |
| } | |
| return false | |
| } | |
| /** | |
| * @description Compare the stored number with a the | |
| * the argument, checking if they are equal | |
| * _only to provide access without instantiating_ | |
| * | |
| * @param {Number} number2 The number to be compared | |
| * @returns {Boolean} true if equal, false if different | |
| * | |
| * @example NumChecks.isEqual(10, 10) -> true | |
| * @example NumChecks.isEqual(6, 6) -> false | |
| */ | |
| static isEqual(a, b){ | |
| return new NumChecks(a).isEqual(b) | |
| } | |
| /** | |
| * @description Checks if the given number is even | |
| * or odd | |
| * @param {Number} number to check | |
| * @returns {Number} 1 if even, -1 if odd | |
| * | |
| * @example new NumChecks(10).evenOdd() -> 1 | |
| * @example new NumChecks(9).evenOdd() -> -1 | |
| */ | |
| evenOdd(){ | |
| if(this.number % 2 == 0){ | |
| return 1 | |
| } | |
| else{ | |
| return -1 | |
| } | |
| } | |
| /** | |
| * @description Make math operations with stored number | |
| * @param {String} symbol Operation symbol [+|-|/|*|%] | |
| * @param {Number} value Value to be calculated | |
| * @param {Boolean} invertOrder Invert order for calculation | |
| * | |
| * @returns {Number} Operation results | |
| * @example new NumChecks(10).opearation("+", 1) -> 11 | |
| */ | |
| operate(symbol, value, invertOrder=false){ | |
| if(typeof symbol !== "string" || !(symbol in this.operations)){ | |
| return new Error(`Operation "${symbol}" is invalid, use .getOperations() to see available operations`) | |
| } | |
| if(!isNaN(value)){ value = Number(value) } | |
| else{ return new Error("value param must be a number"); } | |
| let [a, b] = [this.number, value]; | |
| if(invertOrder) [a, b] = [b, a]; | |
| this.number = this.operations[symbol](a, b); | |
| return this.number; | |
| } | |
| /** | |
| * @description Get available operations | |
| * @returns {Array<String>} Array of available operations | |
| */ | |
| getOperations(){ | |
| return Object.keys(this.operations); | |
| } | |
| /** | |
| * @description Check if a operations is available | |
| * @param {String} symbol Operation symbol to check | |
| * @returns {Boolean} true if is available, false if not | |
| */ | |
| hasOperation(symbol){ | |
| return symbol in this.operations; | |
| } | |
| /** | |
| * @description Remove a operations from object | |
| * @param {String} symbol Operation symbol to remove | |
| * @returns {Function} Removed operation function | |
| */ | |
| removeOperation(symbol){ | |
| if(this.hasOperation(symbol)){ | |
| let op = this.operations[symbol]; | |
| delete this.operations[symbol]; | |
| return op; | |
| } | |
| return new Error(`No operation with symbol "${symbol}"`) | |
| } | |
| /** | |
| * @description isNaN wrapper for better undestating | |
| * @param {*} x Any value to check if is a number | |
| * @returns {Boolean} true if is a number, false if not | |
| * | |
| * @example NumChecks.isNumber(10) -> true | |
| * @example NumChecks.isNumber("a") -> false | |
| */ | |
| static isNumber(x){ | |
| return !isNaN(x) | |
| } | |
| /** | |
| * @description Checks if the given number is odd | |
| * @param {Number} number to check | |
| * @returns {Boolean} true if is a odd, false if even | |
| * | |
| * @example NumChecks.isOdd(9) -> true | |
| * @example NumChecks.isOdd(10) -> false | |
| */ | |
| static isOdd(number){ | |
| if(number % 2 == 0){ | |
| return false | |
| } | |
| return true | |
| } | |
| /** | |
| * @description Checks if the given number is even | |
| * @param {Number} number to check | |
| * @returns {Boolean} true if is a even, false if odd | |
| * | |
| * @example NumChecks.isOdd(9) -> true | |
| * @example NumChecks.isOdd(10) -> false | |
| */ | |
| static isEven(number){ | |
| if(number % 2 == 0){ | |
| return true | |
| } | |
| return false | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment