From 5518fdbe9190561abf48c84f109311ca59651cbf Mon Sep 17 00:00:00 2001 From: Aerex Date: Sun, 1 Dec 2019 12:30:20 -0500 Subject: [PATCH] refactor: Renamed js to nodejs - feat: Added util module --- reverse-integer/{js => nodejs}/index.js | 11 ++++++++--- string-to-integer-aoi/{js => nodejs}/index.js | 7 +------ util/js/index.js | 8 ++++++++ zig-zag-conversion/{js => nodejs}/index.js | 10 +++++++--- 4 files changed, 24 insertions(+), 12 deletions(-) rename reverse-integer/{js => nodejs}/index.js (83%) rename string-to-integer-aoi/{js => nodejs}/index.js (96%) create mode 100644 util/js/index.js rename zig-zag-conversion/{js => nodejs}/index.js (88%) diff --git a/reverse-integer/js/index.js b/reverse-integer/nodejs/index.js similarity index 83% rename from reverse-integer/js/index.js rename to reverse-integer/nodejs/index.js index 81ead88..7f0324a 100644 --- a/reverse-integer/js/index.js +++ b/reverse-integer/nodejs/index.js @@ -17,6 +17,8 @@ Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. */ +const { assert } = require('../../util/js'); + /** * @param {number} x * @return {number} @@ -40,18 +42,21 @@ const reverse = function(x) { * Output: 321 */ x = 123; -console.log(reverse(x)); +sol = 321; +assert(reverse(x) === sol, 'Output: 321'); /* Test Case 2 * Input: -123 * Output: -321 */ x = -123; -console.log(reverse(x)); +sol = -321 +assert(reverse(x) === sol, 'Output: -321'); /* Test Case 3 * Input: 120 * Output: 21 */ x = 120; -console.log(reverse(x)); +sol = 21; +assert(reverse(x) == sol, 'Output: 21'); diff --git a/string-to-integer-aoi/js/index.js b/string-to-integer-aoi/nodejs/index.js similarity index 96% rename from string-to-integer-aoi/js/index.js rename to string-to-integer-aoi/nodejs/index.js index 629cc66..99b3ad0 100644 --- a/string-to-integer-aoi/js/index.js +++ b/string-to-integer-aoi/nodejs/index.js @@ -17,12 +17,7 @@ * Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. */ -const assert = function(condition, message) { - if(!condition) { - console.error(` ${message}`); - } - console.log(` ${message}`); -} +const { assert } = require('../../util/js'); /** * @param {string} str diff --git a/util/js/index.js b/util/js/index.js new file mode 100644 index 0000000..c3b76a6 --- /dev/null +++ b/util/js/index.js @@ -0,0 +1,8 @@ +module.exports.assert = (condition, message) => { + if(condition) { + console.log(` ${message}`); + } + else { + console.error(` ${message}`); + } +} diff --git a/zig-zag-conversion/js/index.js b/zig-zag-conversion/nodejs/index.js similarity index 88% rename from zig-zag-conversion/js/index.js rename to zig-zag-conversion/nodejs/index.js index 3ca0d61..21166e0 100644 --- a/zig-zag-conversion/js/index.js +++ b/zig-zag-conversion/nodejs/index.js @@ -25,6 +25,7 @@ Y A H R P I */ +const { assert } = require('../../util/js'); /** * @param {string} s * @param {number} numRows @@ -95,7 +96,8 @@ let s, numRows; s = 'PAYPALISHIRING'; numRows = 3; -console.log(convert(s, numRows)); +sol = 'PAHNAPLSIIGYIR'; +assert(convert(s, numRows) == sol, 'Output: "PAHNAPLSIIGYIR"'); /* Test Case 2 * Input: s = "PAYPALISHIRING", numRows = 4 @@ -103,7 +105,8 @@ console.log(convert(s, numRows)); */ s = 'PAYPALISHIRING'; numRows = 4; -console.log(convert(s, numRows)); +sol = 'PINALSIGYAHRPI'; +assert(convert(s, numRows) == sol, 'Output: "PINALSIGYAHRPI"'); /* Test Case 3 * Input: s = "AB", numRows = 1 @@ -111,4 +114,5 @@ console.log(convert(s, numRows)); */ s = 'AB'; numRows = 1; -console.log(convert(s, numRows)); +sol = 'AB'; +assert(convert(s, numRows) == sol, 'Output: "AB"');