From f4e651407f77cbb219de52285994fb33d8bafe03 Mon Sep 17 00:00:00 2001 From: dianprsty Date: Wed, 27 Sep 2023 17:20:37 +0700 Subject: [PATCH 1/4] refactor : move dom code to adapter --- pages/functional/adapter/factorial.js | 17 ++ pages/functional/adapter/fibonacci.js | 19 +++ pages/functional/adapter/fizz-buzz.js | 27 ++++ pages/functional/adapter/palindrome.js | 17 ++ pages/functional/factorial.html | 136 ++++++++-------- pages/functional/fibonacci.html | 134 ++++++++-------- pages/functional/fizz-buzz.html | 80 +++++----- pages/functional/js/factorial.js | 124 +++++++-------- pages/functional/js/fibonacci.js | 160 +++++++++---------- pages/functional/js/fizz-buzz.js | 86 ++++------ pages/functional/js/palindrome.js | 203 ++++++++++++------------ pages/functional/palindrome.html | 140 ++++++++--------- pages/oop/adapter/factorial.js | 17 ++ pages/oop/adapter/fibonacci.js | 21 +++ pages/oop/adapter/fizz-buzz.js | 27 ++++ pages/oop/adapter/palindrome.js | 17 ++ pages/oop/factorial.html | 136 ++++++++-------- pages/oop/fibonacci.html | 134 ++++++++-------- pages/oop/fizz-buzz.html | 80 +++++----- pages/oop/js/Factorial.js | 132 +++++++--------- pages/oop/js/Fibonacci.js | 160 +++++++++---------- pages/oop/js/FizzBuzz.js | 100 +++++------- pages/oop/js/Palindrome.js | 210 ++++++++++++------------- pages/oop/palindrome.html | 140 ++++++++--------- 24 files changed, 1173 insertions(+), 1144 deletions(-) create mode 100644 pages/functional/adapter/factorial.js create mode 100644 pages/functional/adapter/fibonacci.js create mode 100644 pages/functional/adapter/fizz-buzz.js create mode 100644 pages/functional/adapter/palindrome.js create mode 100644 pages/oop/adapter/factorial.js create mode 100644 pages/oop/adapter/fibonacci.js create mode 100644 pages/oop/adapter/fizz-buzz.js create mode 100644 pages/oop/adapter/palindrome.js diff --git a/pages/functional/adapter/factorial.js b/pages/functional/adapter/factorial.js new file mode 100644 index 0000000..7d50526 --- /dev/null +++ b/pages/functional/adapter/factorial.js @@ -0,0 +1,17 @@ +import { countFactorial } from "../js/factorial.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const n = event.target["n"].value; + const method = event.target["method"].value; + + const result = countFactorial(n, method); + + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/functional/adapter/fibonacci.js b/pages/functional/adapter/fibonacci.js new file mode 100644 index 0000000..7fe2bea --- /dev/null +++ b/pages/functional/adapter/fibonacci.js @@ -0,0 +1,19 @@ +import { generateFibonacci } from "../js/fibonacci.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const sequence = event.target["sequence"].value; + const method = event.target["method"].value; + + const numbers = generateFibonacci(sequence, method); + + document.getElementById("result").innerHTML = numbers + .map(number => `
${number}
`) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/functional/adapter/fizz-buzz.js b/pages/functional/adapter/fizz-buzz.js new file mode 100644 index 0000000..4648bdc --- /dev/null +++ b/pages/functional/adapter/fizz-buzz.js @@ -0,0 +1,27 @@ +import { generateFizzBuzz } from "../js/fizz-buzz.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const sequence = event.target["sequence"].value; + + const fizzBuzzs = generateFizzBuzz(sequence); + + document.getElementById("result").innerHTML = fizzBuzzs + .map( + (fizzBuzz, index) => + `
${index + 1} ${fizzBuzz}
`, + ) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/functional/adapter/palindrome.js b/pages/functional/adapter/palindrome.js new file mode 100644 index 0000000..3312671 --- /dev/null +++ b/pages/functional/adapter/palindrome.js @@ -0,0 +1,17 @@ +import { generatePalindromeStatus } from "../js/palindrome.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const word = event.target["word"].value; + const method = event.target["method"].value; + + const result = generatePalindromeStatus(word, method); + + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/functional/factorial.html b/pages/functional/factorial.html index ebf3972..a578709 100644 --- a/pages/functional/factorial.html +++ b/pages/functional/factorial.html @@ -1,68 +1,68 @@ - - - - - - RWID Git - Factorial - - - - - -
-

Factorial (Functional)

-
-
- - -
- -
- - - - -
- - -
- -

- Result: - -

- -

Back to Homepage

-
- - - - + + + + + + RWID Git - Factorial + + + + + +
+

Factorial (Functional)

+
+
+ + +
+ +
+ + + + +
+ + +
+ +

+ Result: - +

+ +

Back to Homepage

+
+ + + + diff --git a/pages/functional/fibonacci.html b/pages/functional/fibonacci.html index d619a16..7306e37 100644 --- a/pages/functional/fibonacci.html +++ b/pages/functional/fibonacci.html @@ -1,67 +1,67 @@ - - - - - - RWID Git - Fibonacci - - - - - -
-

Fibonacci (Functional)

- -
-
- - -
- - -
- - -
- - -
- -

Result:

-
- -

Back to Homepage

-
- - - - + + + + + + RWID Git - Fibonacci + + + + + +
+

Fibonacci (Functional)

+ +
+
+ + +
+ + +
+ + +
+ + +
+ +

Result:

+
+ +

Back to Homepage

+
+ + + + diff --git a/pages/functional/fizz-buzz.html b/pages/functional/fizz-buzz.html index b4a94eb..ae009f2 100644 --- a/pages/functional/fizz-buzz.html +++ b/pages/functional/fizz-buzz.html @@ -1,40 +1,40 @@ - - - - - - RWID Git - Fizz Buzz - - - - - -
-

Fizz Buzz (Functional)

- -
- - - - -
-

Back to Homepage

- -

Result:

-
-
- - - - + + + + + + RWID Git - Fizz Buzz + + + + + +
+

Fizz Buzz (Functional)

+ +
+ + + + +
+

Back to Homepage

+ +

Result:

+
+
+ + + + diff --git a/pages/functional/js/factorial.js b/pages/functional/js/factorial.js index deb63e1..ef2f196 100644 --- a/pages/functional/js/factorial.js +++ b/pages/functional/js/factorial.js @@ -1,67 +1,57 @@ -import { parseNumber } from "../../js/helper.js"; - -/** - * Count factorial number from the given "n" value using loop way. - * - * @param {number} n - */ -function countFactorialUsingLoop(n) { - n = parseNumber(n); - - let result = 1; - - for (let index = n; index > 0; index--) { - result *= index; - } - - return result; -} - -/** - * Count factorial number from the given "n" value using recursive way. - * - * @param {number} n - * @returns {number} - */ -function countFactorialUsingRecursive(n) { - n = parseNumber(n); - - if (n < 2) { - return 1; - } - - return n * countFactorialUsingRecursive(n - 1); -} - -/** - * Count factorial number from the given "n" value. - * - * @param {number} n - * @param {"loop" | "recursive"} method - * @throws {Error} - */ -function countFactorial(n, method) { - if (method == "loop") { - return countFactorialUsingLoop(n); - } else if (method == "recursive") { - return countFactorialUsingRecursive(n); - } else { - throw new Error("Method must be loop or recursive."); - } -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const n = event.target["n"].value; - const method = event.target["method"].value; - - const result = countFactorial(n, method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseNumber } from "../../js/helper.js"; + +/** + * Count factorial number from the given "n" value using loop way. + * + * @param {number} n + */ +function countFactorialUsingLoop(n) { + n = parseNumber(n); + + let result = 1; + + for (let index = n; index > 0; index--) { + result *= index; + } + + return result; +} + +/** + * Count factorial number from the given "n" value using recursive way. + * + * @param {number} n + * @returns {number} + */ +function countFactorialUsingRecursive(n) { + n = parseNumber(n); + + if (n < 2) { + return 1; + } + + return n * countFactorialUsingRecursive(n - 1); +} + +/** + * Count factorial number from the given "n" value. + * + * @param {number} n + * @param {"loop" | "recursive"} method + * @throws {Error} + */ +function countFactorial(n, method) { + if (method == "loop") { + return countFactorialUsingLoop(n); + } else if (method == "recursive") { + return countFactorialUsingRecursive(n); + } else { + throw new Error("Method must be loop or recursive."); + } +} + +export { + countFactorialUsingLoop, + countFactorialUsingRecursive, + countFactorial, +}; diff --git a/pages/functional/js/fibonacci.js b/pages/functional/js/fibonacci.js index edfa5ac..a532d9e 100644 --- a/pages/functional/js/fibonacci.js +++ b/pages/functional/js/fibonacci.js @@ -1,86 +1,74 @@ -import { parseNumber } from "../../js/helper.js"; - -/** - * Create an array filled by fibonacci sequence using loop way. - * - * @param {number} sequence - */ -function generateFibonacciUsingLoop(sequence) { - sequence = parseNumber(sequence); - - let result = []; - let prev = 0; - let next = 1; - - for (let index = 0; index < sequence; index++) { - result.push(prev); - - const current = prev + next; - prev = next; - next = current; - } - - return result; -} - -/** - * Create an array filled by fibonacci sequence using recursive way. - * - * @param {number} sequence - */ -function generateFibonacciUsingRecursive(sequence) { - /** - * @param {number} n - * @returns {number} - */ - function fibonacci(n) { - return n < 1 ? 0 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2); - } - - sequence = parseNumber(sequence); - - let result = []; - - for (let index = 0; index < sequence; index++) { - const current = fibonacci(index); - - result.push(current); - } - - return result; -} - -/** - * Create an array filled by fibonacci sequence. - * - * @param {number} sequence - * @param {"loop"| "recursive"} method - * @throws {Error} - */ -function generateFibonacci(sequence, method) { - if (method === "loop") { - return generateFibonacciUsingLoop(sequence); - } else if (method === "recursive") { - return generateFibonacciUsingRecursive(sequence); - } else { - throw new Error("Method must be loop or recursive."); - } -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - const method = event.target["method"].value; - - const numbers = generateFibonacci(sequence, method); - - document.getElementById("result").innerHTML = numbers - .map(number => `
${number}
`) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseNumber } from "../../js/helper.js"; + +/** + * Create an array filled by fibonacci sequence using loop way. + * + * @param {number} sequence + */ +function generateFibonacciUsingLoop(sequence) { + sequence = parseNumber(sequence); + + let result = []; + let prev = 0; + let next = 1; + + for (let index = 0; index < sequence; index++) { + result.push(prev); + + const current = prev + next; + prev = next; + next = current; + } + + return result; +} + +/** + * Create an array filled by fibonacci sequence using recursive way. + * + * @param {number} sequence + */ +function generateFibonacciUsingRecursive(sequence) { + /** + * @param {number} n + * @returns {number} + */ + function fibonacci(n) { + return n < 1 ? 0 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2); + } + + sequence = parseNumber(sequence); + + let result = []; + + for (let index = 0; index < sequence; index++) { + const current = fibonacci(index); + + result.push(current); + } + + return result; +} + +/** + * Create an array filled by fibonacci sequence. + * + * @param {number} sequence + * @param {"loop"| "recursive"} method + * @throws {Error} + */ +function generateFibonacci(sequence, method) { + if (method === "loop") { + return generateFibonacciUsingLoop(sequence); + } else if (method === "recursive") { + return generateFibonacciUsingRecursive(sequence); + } else { + throw new Error("Method must be loop or recursive."); + } +} + +export { + generateFibonacciUsingLoop, + generateFibonacciUsingRecursive, + generateFibonacci, +}; diff --git a/pages/functional/js/fizz-buzz.js b/pages/functional/js/fizz-buzz.js index e6bd12e..9c39e2d 100644 --- a/pages/functional/js/fizz-buzz.js +++ b/pages/functional/js/fizz-buzz.js @@ -1,55 +1,31 @@ -import { parseNumber } from "../../js/helper.js"; - -/** - * Create an array filled by "fizz", "buzz", or "fizz buzz" based on - * this requirement below (highest priority from above). - * - * Muliples of 4 or 7: "fizz buzz" - * Odd sequence: "fizz" - * Even sequence: "buzz" - * - * @param {number} sequence - */ -function generateFizzBuzz(sequence) { - sequence = parseNumber(sequence); - - let result = []; - - for (let index = 1; index <= sequence; index++) { - if (index % 4 === 0 || index % 7 === 0) { - result.push("fizz buzz"); - } else if (index % 2 === 1) { - result.push("fizz"); - } else if (index % 2 === 0) { - result.push("buzz"); - } - } - - return result; -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - - const fizzBuzzs = generateFizzBuzz(sequence); - - document.getElementById("result").innerHTML = fizzBuzzs - .map( - (fizzBuzz, index) => - `
${index + 1} ${fizzBuzz}
`, - ) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseNumber } from "../../js/helper.js"; + +/** + * Create an array filled by "fizz", "buzz", or "fizz buzz" based on + * this requirement below (highest priority from above). + * + * Muliples of 4 or 7: "fizz buzz" + * Odd sequence: "fizz" + * Even sequence: "buzz" + * + * @param {number} sequence + */ +function generateFizzBuzz(sequence) { + sequence = parseNumber(sequence); + + let result = []; + + for (let index = 1; index <= sequence; index++) { + if (index % 4 === 0 || index % 7 === 0) { + result.push("fizz buzz"); + } else if (index % 2 === 1) { + result.push("fizz"); + } else if (index % 2 === 0) { + result.push("buzz"); + } + } + + return result; +} + +export { generateFizzBuzz }; diff --git a/pages/functional/js/palindrome.js b/pages/functional/js/palindrome.js index 3007919..4baa972 100644 --- a/pages/functional/js/palindrome.js +++ b/pages/functional/js/palindrome.js @@ -1,106 +1,97 @@ -import { parseString } from "../../js/helper.js"; - -/** - * Determine whether the given value is a palindrome or not using reverse way. - * - * @param {string} value - */ -function isPalindromeUsingReverse(value) { - value = parseString(value); - - let newValue = ""; - - for (let index = value.length - 1; index >= 0; index--) { - newValue += value[index]; - } - - return value === newValue; -} - -/** - * Determine whether the given value is a palindrome or not using loop way. - * - * @param {string} value - */ -function isPalindromeUsingLoop(value) { - value = parseString(value); - - for (let index = 0; index < Math.floor(value.length / 2); index++) { - const lastCharacterIndex = value.length - (index + 1); - - const firstCharacter = value[index]; - const lastCharacter = value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } - } - - return true; -} - -/** - * Determine whether the given value is a palindrome or not using recursive way. - * - * @param {string} value - * @param {number} index - */ -function isPalindromeUsingRecursive(value, index = 0) { - value = parseString(value); - - if (index < Math.floor(value.length / 2)) { - const lastCharacterIndex = value.length - (index + 1); - - const firstCharacter = value[index]; - const lastCharacter = value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } - - return isPalindromeUsingRecursive(value, index + 1); - } - - return true; -} - -/** - * Return palindrome human-readable description. - * - * @param {string} word - * @param {"reverse" | "loop" | "recursive"} method - * @throws {Error} - */ -function generatePalindromeStatus(word, method) { - let isPalindrome; - - if (method === "reverse") { - isPalindrome = isPalindromeUsingReverse(word); - } else if (method === "loop") { - isPalindrome = isPalindromeUsingLoop(word); - } else if (method === "recursive") { - isPalindrome = isPalindromeUsingRecursive(word); - } else { - throw new Error("Method must be reverse, loop, or recursive."); - } - - return isPalindrome - ? "Yes, this word is a palindrome." - : "No, this word is not a palindrome."; -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const word = event.target["word"].value; - const method = event.target["method"].value; - - const result = generatePalindromeStatus(word, method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseString } from "../../js/helper.js"; + +/** + * Determine whether the given value is a palindrome or not using reverse way. + * + * @param {string} value + */ +function isPalindromeUsingReverse(value) { + value = parseString(value); + + let newValue = ""; + + for (let index = value.length - 1; index >= 0; index--) { + newValue += value[index]; + } + + return value === newValue; +} + +/** + * Determine whether the given value is a palindrome or not using loop way. + * + * @param {string} value + */ +function isPalindromeUsingLoop(value) { + value = parseString(value); + + for (let index = 0; index < Math.floor(value.length / 2); index++) { + const lastCharacterIndex = value.length - (index + 1); + + const firstCharacter = value[index]; + const lastCharacter = value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + } + + return true; +} + +/** + * Determine whether the given value is a palindrome or not using recursive way. + * + * @param {string} value + * @param {number} index + */ +function isPalindromeUsingRecursive(value, index = 0) { + value = parseString(value); + + if (index < Math.floor(value.length / 2)) { + const lastCharacterIndex = value.length - (index + 1); + + const firstCharacter = value[index]; + const lastCharacter = value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + + return isPalindromeUsingRecursive(value, index + 1); + } + + return true; +} + +/** + * Return palindrome human-readable description. + * + * @param {string} word + * @param {"reverse" | "loop" | "recursive"} method + * @throws {Error} + */ +function generatePalindromeStatus(word, method) { + let isPalindrome; + + if (method === "reverse") { + isPalindrome = isPalindromeUsingReverse(word); + } else if (method === "loop") { + isPalindrome = isPalindromeUsingLoop(word); + } else if (method === "recursive") { + isPalindrome = isPalindromeUsingRecursive(word); + } else { + throw new Error("Method must be reverse, loop, or recursive."); + } + + return isPalindrome + ? "Yes, this word is a palindrome." + : "No, this word is not a palindrome."; +} + +export { + isPalindromeUsingLoop, + isPalindromeUsingRecursive, + isPalindromeUsingReverse, + generatePalindromeStatus, +}; diff --git a/pages/functional/palindrome.html b/pages/functional/palindrome.html index 82d8d1c..d3e1ca1 100644 --- a/pages/functional/palindrome.html +++ b/pages/functional/palindrome.html @@ -1,70 +1,70 @@ - - - - - - RWID Git - Palindrome - - - - - -
-

Palindrome (Functional)

- -
-
- - -
- -
- - - - -
- -
- -

- Result: - -

- -

Back to Homepage

-
- - - - + + + + + + RWID Git - Palindrome + + + + + +
+

Palindrome (Functional)

+ +
+
+ + +
+ +
+ + + + +
+ +
+ +

+ Result: - +

+ +

Back to Homepage

+
+ + + + diff --git a/pages/oop/adapter/factorial.js b/pages/oop/adapter/factorial.js new file mode 100644 index 0000000..647f23d --- /dev/null +++ b/pages/oop/adapter/factorial.js @@ -0,0 +1,17 @@ +import { Factorial } from "../js/Factorial.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const n = event.target["n"].value; + const method = event.target["method"].value; + + const result = new Factorial(n).count(method); + + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/oop/adapter/fibonacci.js b/pages/oop/adapter/fibonacci.js new file mode 100644 index 0000000..4eae253 --- /dev/null +++ b/pages/oop/adapter/fibonacci.js @@ -0,0 +1,21 @@ +import { Fibonacci } from "../js/fibonacci.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const sequence = event.target["sequence"].value; + const method = event.target["method"].value; + + const numbers = new Fibonacci(sequence).generate(method); + + document.getElementById("result").innerHTML = document.getElementById( + "result", + ).innerHTML = numbers + .map(number => `
${number}
`) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/oop/adapter/fizz-buzz.js b/pages/oop/adapter/fizz-buzz.js new file mode 100644 index 0000000..d4e5be2 --- /dev/null +++ b/pages/oop/adapter/fizz-buzz.js @@ -0,0 +1,27 @@ +import { FizzBuzz } from "../js/FizzBuzz.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const sequence = event.target["sequence"].value; + + const fizzBuzzs = new FizzBuzz(sequence).generate(); + + document.getElementById("result").innerHTML = fizzBuzzs + .map( + (fizzBuzz, index) => + `
${index + 1} ${fizzBuzz}
`, + ) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/oop/adapter/palindrome.js b/pages/oop/adapter/palindrome.js new file mode 100644 index 0000000..763deaa --- /dev/null +++ b/pages/oop/adapter/palindrome.js @@ -0,0 +1,17 @@ +import { Palindrome } from "../js/palindrome.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const word = event.target["word"].value; + const method = event.target["method"].value; + + const result = new Palindrome(word).generateStatus(method); + + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/oop/factorial.html b/pages/oop/factorial.html index c947ef6..35d9686 100644 --- a/pages/oop/factorial.html +++ b/pages/oop/factorial.html @@ -1,68 +1,68 @@ - - - - - - RWID Git - Factorial - - - - - -
-

Factorial (OOP)

-
-
- - -
- -
- - - - -
- - -
- -

- Result: - -

- -

Back to Homepage

-
- - - - + + + + + + RWID Git - Factorial + + + + + +
+

Factorial (OOP)

+
+
+ + +
+ +
+ + + + +
+ + +
+ +

+ Result: - +

+ +

Back to Homepage

+
+ + + + diff --git a/pages/oop/fibonacci.html b/pages/oop/fibonacci.html index 0819d83..27eca19 100644 --- a/pages/oop/fibonacci.html +++ b/pages/oop/fibonacci.html @@ -1,67 +1,67 @@ - - - - - - RWID Git - Fibonacci - - - - - -
-

Fibonacci (OOP)

- -
-
- - -
- - -
- - -
- - -
- -

Result:

-
- -

Back to Homepage

-
- - - - + + + + + + RWID Git - Fibonacci + + + + + +
+

Fibonacci (OOP)

+ +
+
+ + +
+ + +
+ + +
+ + +
+ +

Result:

+
+ +

Back to Homepage

+
+ + + + diff --git a/pages/oop/fizz-buzz.html b/pages/oop/fizz-buzz.html index b9d6083..52265fa 100644 --- a/pages/oop/fizz-buzz.html +++ b/pages/oop/fizz-buzz.html @@ -1,40 +1,40 @@ - - - - - - RWID Git - Fizz Buzz - - - - - -
-

Fizz Buzz (OOP)

- -
- - - - -
- -

Back to Homepage

-

Result:

-
-
- - - - + + + + + + RWID Git - Fizz Buzz + + + + + +
+

Fizz Buzz (OOP)

+ +
+ + + + +
+ +

Back to Homepage

+

Result:

+
+
+ + + + diff --git a/pages/oop/js/Factorial.js b/pages/oop/js/Factorial.js index 77a83a8..d4fd5db 100644 --- a/pages/oop/js/Factorial.js +++ b/pages/oop/js/Factorial.js @@ -1,74 +1,58 @@ -import { parseNumber } from "../../js/helper.js"; - -export class Factorial { - /** @type {number} */ - n; - - /** - * @param {number} n - */ - constructor(n) { - this.n = parseNumber(n); - } - - /** - * Count factorial number from the given "n" value using loop way. - */ - countUsingLoop() { - let result = 1; - - for (let index = this.n; index > 0; index--) { - result = result * index; - } - - return result; - } - - /** - * Count factorial number from the given "n" value using recursive way. - * - * @param {number | undefined} n - * @returns {number} - */ - countUsingRecursive(n = undefined) { - n ||= this.n; - - if (n < 2) { - return 1; - } - - return n * this.countUsingRecursive(n - 1); - } - - /** - * Count factorial number from the given "n" value. - * - * @param {"loop" | "recursive"} method - * @throws {Error} - */ - count(method) { - if (method == "loop") { - return this.countUsingLoop(); - } else if (method == "recursive") { - return this.countUsingRecursive(); - } else { - throw new Error("Method must be loop or recursive."); - } - } -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const n = event.target["n"].value; - const method = event.target["method"].value; - - const result = new Factorial(n).count(method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseNumber } from "../../js/helper.js"; + +export class Factorial { + /** @type {number} */ + n; + + /** + * @param {number} n + */ + constructor(n) { + this.n = parseNumber(n); + } + + /** + * Count factorial number from the given "n" value using loop way. + */ + countUsingLoop() { + let result = 1; + + for (let index = this.n; index > 0; index--) { + result = result * index; + } + + return result; + } + + /** + * Count factorial number from the given "n" value using recursive way. + * + * @param {number | undefined} n + * @returns {number} + */ + countUsingRecursive(n = undefined) { + n ||= this.n; + + if (n < 2) { + return 1; + } + + return n * this.countUsingRecursive(n - 1); + } + + /** + * Count factorial number from the given "n" value. + * + * @param {"loop" | "recursive"} method + * @throws {Error} + */ + count(method) { + if (method == "loop") { + return this.countUsingLoop(); + } else if (method == "recursive") { + return this.countUsingRecursive(); + } else { + throw new Error("Method must be loop or recursive."); + } + } +} diff --git a/pages/oop/js/Fibonacci.js b/pages/oop/js/Fibonacci.js index 555a4f3..9235c7d 100644 --- a/pages/oop/js/Fibonacci.js +++ b/pages/oop/js/Fibonacci.js @@ -1,90 +1,70 @@ -import { parseNumber } from "../../js/helper.js"; - -export class Fibonacci { - /** @type {number} */ - sequence; - - /** - * @param {number} sequence - */ - constructor(sequence) { - this.sequence = parseNumber(sequence); - } - - /** - * @param {number} n - * @returns {number} - */ - static get(n) { - return n < 1 ? 0 : n <= 2 ? 1 : this.get(n - 1) + this.get(n - 2); - } - - /** - * Create an array filled by fibonacci sequence using loop way. - */ - generateUsingLoop() { - let result = []; - let prev = 0; - let next = 1; - - for (let index = 0; index < this.sequence; index++) { - result.push(prev); - - const current = prev + next; - prev = next; - next = current; - } - - return result; - } - - /** - * Create an array filled by fibonacci sequence using recursive way. - */ - generateUsingRecursive() { - let result = []; - - for (let index = 0; index < this.sequence; index++) { - const current = Fibonacci.get(index); - - result.push(current); - } - - return result; - } - - /** - * Create an array filled by fibonacci sequence. - * - * @param {"loop" | "recursive"} method - */ - generate(method) { - if (method === "loop") { - return this.generateUsingLoop(); - } else if (method === "recursive") { - return this.generateUsingRecursive(); - } else { - throw new Error("Method must be loop or recursive."); - } - } -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - const method = event.target["method"].value; - - const numbers = new Fibonacci(sequence).generate(method); - - document.getElementById("result").innerHTML = document.getElementById( - "result", - ).innerHTML = numbers - .map(number => `
${number}
`) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseNumber } from "../../js/helper.js"; + +export class Fibonacci { + /** @type {number} */ + sequence; + + /** + * @param {number} sequence + */ + constructor(sequence) { + this.sequence = parseNumber(sequence); + } + + /** + * @param {number} n + * @returns {number} + */ + static get(n) { + return n < 1 ? 0 : n <= 2 ? 1 : this.get(n - 1) + this.get(n - 2); + } + + /** + * Create an array filled by fibonacci sequence using loop way. + */ + generateUsingLoop() { + let result = []; + let prev = 0; + let next = 1; + + for (let index = 0; index < this.sequence; index++) { + result.push(prev); + + const current = prev + next; + prev = next; + next = current; + } + + return result; + } + + /** + * Create an array filled by fibonacci sequence using recursive way. + */ + generateUsingRecursive() { + let result = []; + + for (let index = 0; index < this.sequence; index++) { + const current = Fibonacci.get(index); + + result.push(current); + } + + return result; + } + + /** + * Create an array filled by fibonacci sequence. + * + * @param {"loop" | "recursive"} method + */ + generate(method) { + if (method === "loop") { + return this.generateUsingLoop(); + } else if (method === "recursive") { + return this.generateUsingRecursive(); + } else { + throw new Error("Method must be loop or recursive."); + } + } +} diff --git a/pages/oop/js/FizzBuzz.js b/pages/oop/js/FizzBuzz.js index a9d1e6b..7cea3af 100644 --- a/pages/oop/js/FizzBuzz.js +++ b/pages/oop/js/FizzBuzz.js @@ -1,63 +1,37 @@ -import { parseNumber } from "../../js/helper.js"; - -export class FizzBuzz { - /** @type {number} */ - sequence; - - /** - * @param {number} sequence - */ - constructor(sequence) { - this.sequence = parseNumber(sequence); - } - - /** - * Create an array filled by "fizz", "buzz", or "fizz buzz" based on - * this requirement below (highest priority from above). - * - * Muliples of 4 or 7: "fizz buzz" - * Odd sequence: "fizz" - * Even sequence: "buzz" - */ - generate() { - let result = []; - - for (let index = 1; index <= this.sequence; index++) { - if (index % 4 === 0 || index % 7 === 0) { - result.push("fizz buzz"); - } else if (index % 2 === 1) { - result.push("fizz"); - } else if (index % 2 === 0) { - result.push("buzz"); - } - } - - return result; - } -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - - const fizzBuzzs = new FizzBuzz(sequence).generate(); - - document.getElementById("result").innerHTML = fizzBuzzs - .map( - (fizzBuzz, index) => - `
${index + 1} ${fizzBuzz}
`, - ) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseNumber } from "../../js/helper.js"; + +export class FizzBuzz { + /** @type {number} */ + sequence; + + /** + * @param {number} sequence + */ + constructor(sequence) { + this.sequence = parseNumber(sequence); + } + + /** + * Create an array filled by "fizz", "buzz", or "fizz buzz" based on + * this requirement below (highest priority from above). + * + * Muliples of 4 or 7: "fizz buzz" + * Odd sequence: "fizz" + * Even sequence: "buzz" + */ + generate() { + let result = []; + + for (let index = 1; index <= this.sequence; index++) { + if (index % 4 === 0 || index % 7 === 0) { + result.push("fizz buzz"); + } else if (index % 2 === 1) { + result.push("fizz"); + } else if (index % 2 === 0) { + result.push("buzz"); + } + } + + return result; + } +} diff --git a/pages/oop/js/Palindrome.js b/pages/oop/js/Palindrome.js index af85a2c..2c0cc8f 100644 --- a/pages/oop/js/Palindrome.js +++ b/pages/oop/js/Palindrome.js @@ -1,113 +1,97 @@ -import { parseString } from "../../js/helper.js"; - -export class Palindrome { - /** @type {string} */ - value; - - /** - * @param {string} value - */ - constructor(value) { - this.value = parseString(value); - } - - /** - * Determine whether the given value is a palindrome or not using reverse way. - */ - evaluateUsingReverse() { - let newValue = ""; - - for (let index = this.value.length - 1; index >= 0; index--) { - newValue += this.value[index]; - } - - return this.value === newValue; - } - - /** - * Determine whether the given value is a palindrome or not using loop way. - */ - evaluateUsingLoop() { - for (let index = 0; index < Math.floor(this.value.length / 2); index++) { - const lastCharacterIndex = this.value.length - (index + 1); - - const firstCharacter = this.value[index]; - const lastCharacter = this.value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } - } - - return true; - } - - /** - * Determine whether the given value is a palindrome or not using recursive way. - * - * @param {number | undefined} index - */ - evaluateUsingRecursive(index = undefined) { - index ||= 0; - - if (index < Math.floor(this.value.length / 2)) { - const lastCharacterIndex = this.value.length - (index + 1); - - const firstCharacter = this.value[index]; - const lastCharacter = this.value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } - - return this.evaluateUsingRecursive(this.value, index + 1); - } - - return true; - } - - /** - * Determine whether the given value is a palindrome or not. - * - * @typedef {"reverse" | "loop" | "recursive"} Method - * @param {Method} method - */ - evaluate(method) { - if (method === "reverse") { - return this.evaluateUsingReverse(); - } else if (method === "loop") { - return this.evaluateUsingLoop(); - } else if (method === "recursive") { - return this.evaluateUsingRecursive(); - } else { - throw new Error("Method must be reverse, loop, or recursive."); - } - } - - /** - * Return palindrome human-readable description. - * - * @param {Method} method - */ - generateStatus(method) { - return this.evaluate(method) - ? "Yes, this word is a palindrome." - : "No, this word is not a palindrome."; - } -} - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const word = event.target["word"].value; - const method = event.target["method"].value; - - const result = new Palindrome(word).generateStatus(method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { parseString } from "../../js/helper.js"; + +export class Palindrome { + /** @type {string} */ + value; + + /** + * @param {string} value + */ + constructor(value) { + this.value = parseString(value); + } + + /** + * Determine whether the given value is a palindrome or not using reverse way. + */ + evaluateUsingReverse() { + let newValue = ""; + + for (let index = this.value.length - 1; index >= 0; index--) { + newValue += this.value[index]; + } + + return this.value === newValue; + } + + /** + * Determine whether the given value is a palindrome or not using loop way. + */ + evaluateUsingLoop() { + for (let index = 0; index < Math.floor(this.value.length / 2); index++) { + const lastCharacterIndex = this.value.length - (index + 1); + + const firstCharacter = this.value[index]; + const lastCharacter = this.value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + } + + return true; + } + + /** + * Determine whether the given value is a palindrome or not using recursive way. + * + * @param {number | undefined} index + */ + evaluateUsingRecursive(index = undefined) { + index ||= 0; + + if (index < Math.floor(this.value.length / 2)) { + const lastCharacterIndex = this.value.length - (index + 1); + + const firstCharacter = this.value[index]; + const lastCharacter = this.value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + + return this.evaluateUsingRecursive(this.value, index + 1); + } + + return true; + } + + /** + * Determine whether the given value is a palindrome or not. + * + * @typedef {"reverse" | "loop" | "recursive"} Method + * @param {Method} method + */ + evaluate(method) { + if (method === "reverse") { + return this.evaluateUsingReverse(); + } else if (method === "loop") { + return this.evaluateUsingLoop(); + } else if (method === "recursive") { + return this.evaluateUsingRecursive(); + } else { + throw new Error("Method must be reverse, loop, or recursive."); + } + } + + /** + * Return palindrome human-readable description. + * + * @param {Method} method + */ + generateStatus(method) { + return this.evaluate(method) + ? "Yes, this word is a palindrome." + : "No, this word is not a palindrome."; + } +} diff --git a/pages/oop/palindrome.html b/pages/oop/palindrome.html index e2339b6..3ae1368 100644 --- a/pages/oop/palindrome.html +++ b/pages/oop/palindrome.html @@ -1,70 +1,70 @@ - - - - - - RWID Git - Palindrome - - - - - -
-

Palindrome (OOP)

- -
-
- - -
- -
- - - - -
- -
- -

- Result: - -

- -

Back to Homepage

-
- - - - + + + + + + RWID Git - Palindrome + + + + + +
+

Palindrome (OOP)

+ +
+
+ + +
+ +
+ + + + +
+ +
+ +

+ Result: - +

+ +

Back to Homepage

+
+ + + + From 070d5ea1f676125093f0f6b04216d2cc249e8919 Mon Sep 17 00:00:00 2001 From: dianprsty Date: Wed, 27 Sep 2023 17:23:00 +0700 Subject: [PATCH 2/4] feat : add featuere to call challenge1's method from cli --- bin/factorial/handleArgument.js | 31 ++++++++++++++++++++++++++++++ bin/factorial/index.js | 33 ++++++++++++++++++++++++++++++++ bin/fibonacci/handleArgument.js | 31 ++++++++++++++++++++++++++++++ bin/fibonacci/index.js | 33 ++++++++++++++++++++++++++++++++ bin/fizzbuzz/handleArgument.js | 31 ++++++++++++++++++++++++++++++ bin/fizzbuzz/index.js | 28 +++++++++++++++++++++++++++ bin/palindrome/handleArgument.js | 31 ++++++++++++++++++++++++++++++ bin/palindrome/index.js | 30 +++++++++++++++++++++++++++++ bin/shared/utils.js | 3 +++ package.json | 5 ++++- 10 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 bin/factorial/handleArgument.js create mode 100644 bin/factorial/index.js create mode 100644 bin/fibonacci/handleArgument.js create mode 100644 bin/fibonacci/index.js create mode 100644 bin/fizzbuzz/handleArgument.js create mode 100644 bin/fizzbuzz/index.js create mode 100644 bin/palindrome/handleArgument.js create mode 100644 bin/palindrome/index.js create mode 100644 bin/shared/utils.js diff --git a/bin/factorial/handleArgument.js b/bin/factorial/handleArgument.js new file mode 100644 index 0000000..62b6ece --- /dev/null +++ b/bin/factorial/handleArgument.js @@ -0,0 +1,31 @@ +import { destructureArgument, validateArgument } from "../shared/utils.js"; + +export const handleArgument = cliArguments => { + let type = ""; + let method = ""; + let n = 0; + + for (let argument of cliArguments) { + if (!validateArgument(argument)) { + throw new Error(`invalid argument ${argument}`); + } + + const [key, value] = destructureArgument(argument); + + switch (key) { + case "--type": + type = value; + break; + case "--method": + method = value; + break; + case "--n": + n = value; + break; + default: + throw new Error(`invalid argument ${argument}`); + } + } + + return { type, method, n }; +}; diff --git a/bin/factorial/index.js b/bin/factorial/index.js new file mode 100644 index 0000000..5dad5c7 --- /dev/null +++ b/bin/factorial/index.js @@ -0,0 +1,33 @@ +import { countFactorial } from "../../pages/functional/js/factorial.js"; + +import { Factorial } from "../../pages/oop/js/Factorial.js"; + +import { handleArgument } from "./handleArgument.js"; + +const cliArguments = process.argv.slice(2); +const { type, method, n } = handleArgument(cliArguments); +let result = 0; + +if (!["oop", "functional"].includes(type)) { + throw new Error(`invalid type ${type}. must be "oop" or "functional"`); +} + +if (!["loop", "recursive"].includes(method)) { + throw new Error(`invalid method ${method}. must be "loop" or "recursive"`); +} + +if (typeof parseInt(n) != "number") { + throw new Error(`invalid number ${n}. n should be a number"`); +} + +if (type == "functional") { + result = countFactorial(n, method); +} + +if (type == "oop") { + const factorial = new Factorial(n); + result = factorial.count(method); +} + +console.log(`------count factorial using ${method} (${type})------`); +console.log(`factorial of ${n} is ${result}`); diff --git a/bin/fibonacci/handleArgument.js b/bin/fibonacci/handleArgument.js new file mode 100644 index 0000000..ab86854 --- /dev/null +++ b/bin/fibonacci/handleArgument.js @@ -0,0 +1,31 @@ +import { destructureArgument, validateArgument } from "../shared/utils.js"; + +export const handleArgument = cliArguments => { + let type = ""; + let method = ""; + let sequence = 0; + + for (let argument of cliArguments) { + if (!validateArgument(argument)) { + throw new Error(`invalid argument ${argument}`); + } + + const [key, value] = destructureArgument(argument); + + switch (key) { + case "--type": + type = value; + break; + case "--method": + method = value; + break; + case "--sequence": + sequence = value; + break; + default: + throw new Error(`invalid argument ${argument}`); + } + } + + return { type, method, sequence }; +}; diff --git a/bin/fibonacci/index.js b/bin/fibonacci/index.js new file mode 100644 index 0000000..ee26084 --- /dev/null +++ b/bin/fibonacci/index.js @@ -0,0 +1,33 @@ +import { generateFibonacci } from "../../pages/functional/js/fibonacci.js"; +import { Fibonacci } from "../../pages/oop/js/Fibonacci.js"; + +import { handleArgument } from "./handleArgument.js"; + +const cliArguments = process.argv.slice(2); +const { type, method, sequence } = handleArgument(cliArguments); +let result = 0; + +if (!["oop", "functional"].includes(type)) { + throw new Error(`invalid type ${type}. must be "oop" or "functional"`); +} + +if (!["loop", "recursive"].includes(method)) { + throw new Error(`invalid method ${method}. must be "loop" or "recursive"`); +} + +if (typeof parseInt(sequence) != "number") { + throw new Error(`invalid number ${sequence}. n should be a number"`); +} + +if (type == "functional") { + result = generateFibonacci(sequence, method); +} + +if (type == "oop") { + const fibonacci = new Fibonacci(sequence); + result = fibonacci.generate(method); +} + +console.log(`------generate fibonacci using ${method} (${type})------`); +console.log(`fibonacci array with ${sequence} sequence :`); +console.log(result); diff --git a/bin/fizzbuzz/handleArgument.js b/bin/fizzbuzz/handleArgument.js new file mode 100644 index 0000000..ab86854 --- /dev/null +++ b/bin/fizzbuzz/handleArgument.js @@ -0,0 +1,31 @@ +import { destructureArgument, validateArgument } from "../shared/utils.js"; + +export const handleArgument = cliArguments => { + let type = ""; + let method = ""; + let sequence = 0; + + for (let argument of cliArguments) { + if (!validateArgument(argument)) { + throw new Error(`invalid argument ${argument}`); + } + + const [key, value] = destructureArgument(argument); + + switch (key) { + case "--type": + type = value; + break; + case "--method": + method = value; + break; + case "--sequence": + sequence = value; + break; + default: + throw new Error(`invalid argument ${argument}`); + } + } + + return { type, method, sequence }; +}; diff --git a/bin/fizzbuzz/index.js b/bin/fizzbuzz/index.js new file mode 100644 index 0000000..cd672d0 --- /dev/null +++ b/bin/fizzbuzz/index.js @@ -0,0 +1,28 @@ +import { generateFizzBuzz } from "../../pages/functional/js/fizz-buzz.js"; +import { FizzBuzz } from "../../pages/oop/js/FizzBuzz.js"; +import { handleArgument } from "./handleArgument.js"; + +const cliArguments = process.argv.slice(2); +const { type, sequence } = handleArgument(cliArguments); +let result = 0; + +if (!["oop", "functional"].includes(type)) { + throw new Error(`invalid type ${type}. must be "oop" or "functional"`); +} + +if (typeof parseInt(sequence) != "number") { + throw new Error(`invalid number ${sequence}. n should be a number"`); +} + +if (type == "functional") { + result = generateFizzBuzz(sequence); +} + +if (type == "oop") { + const fizzbuzz = new FizzBuzz(sequence); + result = fizzbuzz.generate(); +} + +console.log(`------generate fizzbuzz (${type})------`); +console.log(`fizzbuzz array with ${sequence} sequence :`); +console.log(result); diff --git a/bin/palindrome/handleArgument.js b/bin/palindrome/handleArgument.js new file mode 100644 index 0000000..8228c65 --- /dev/null +++ b/bin/palindrome/handleArgument.js @@ -0,0 +1,31 @@ +import { destructureArgument, validateArgument } from "../shared/utils.js"; + +export const handleArgument = cliArguments => { + let type = ""; + let method = ""; + let word = ""; + + for (let argument of cliArguments) { + if (!validateArgument(argument)) { + throw new Error(`invalid argument ${argument}`); + } + + const [key, value] = destructureArgument(argument); + + switch (key) { + case "--type": + type = value; + break; + case "--method": + method = value; + break; + case "--word": + word = value; + break; + default: + throw new Error(`invalid argument ${argument}`); + } + } + + return { type, method, word }; +}; diff --git a/bin/palindrome/index.js b/bin/palindrome/index.js new file mode 100644 index 0000000..57c4f2d --- /dev/null +++ b/bin/palindrome/index.js @@ -0,0 +1,30 @@ +import { generatePalindromeStatus } from "../../pages/functional/js/palindrome.js"; +import { Palindrome } from "../../pages/oop/js/Palindrome.js"; +import { handleArgument } from "./handleArgument.js"; + +const cliArguments = process.argv.slice(2); +const { type, method, word } = handleArgument(cliArguments); +let result = 0; + +if (!["oop", "functional"].includes(type)) { + throw new Error(`invalid type ${type}. must be "oop" or "functional"`); +} + +if (!["loop", "recursive", "reverse"].includes(method)) { + throw new Error( + `invalid method ${method}. must be "loop", "recursive" or "reverse"`, + ); +} + +if (type == "functional") { + result = generatePalindromeStatus(word, method); +} + +if (type == "oop") { + const palindrome = new Palindrome(word); + result = palindrome.generateStatus(method); +} + +console.log(`------generate palindrom status using ${method} (${type})------`); +console.log(`is ${word} a palindrome?`); +console.log(result); diff --git a/bin/shared/utils.js b/bin/shared/utils.js new file mode 100644 index 0000000..1ce9ccb --- /dev/null +++ b/bin/shared/utils.js @@ -0,0 +1,3 @@ +export const destructureArgument = argument => argument.split("="); + +export const validateArgument = argument => /^--\w+=\w+$/.test(argument); diff --git a/package.json b/package.json index 931c37d..8a1dd86 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,10 @@ "version": "1.0.0", "description": "Learn to code with version control system using Git.", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "factorial": "node bin/factorial/index.js", + "fibonacci": "node bin/fibonacci/index.js", + "fizzbuzz": "node bin/fizzbuzz/index.js", + "palindrome": "node bin/palindrome/index.js" }, "repository": { "type": "git", From 74ce65d8f45151cacf57d4de942e614ee2fd8447 Mon Sep 17 00:00:00 2001 From: dianprsty Date: Fri, 3 Nov 2023 15:06:01 +0700 Subject: [PATCH 3/4] refactor : move js to src and utility to utils --- bin/factorial/handleArgument.js | 31 ------- bin/factorial/index.js | 55 +++++++----- bin/fibonacci/handleArgument.js | 31 ------- bin/fibonacci/index.js | 60 +++++++------ bin/fizzbuzz/handleArgument.js | 31 ------- bin/fizzbuzz/index.js | 64 +++++++------ bin/palindrome/handleArgument.js | 31 ------- bin/palindrome/index.js | 76 +++++++++------- bin/shared/utils.js | 3 - package.json | 14 ++- pages/functional/adapter/factorial.js | 17 ---- pages/functional/adapter/fibonacci.js | 19 ---- pages/functional/adapter/fizz-buzz.js | 27 ------ pages/functional/adapter/palindrome.js | 17 ---- pages/functional/factorial.html | 2 +- pages/functional/fibonacci.html | 2 +- pages/functional/fizz-buzz.html | 2 +- pages/functional/js/factorial.js | 64 +++---------- pages/functional/js/fibonacci.js | 83 +++-------------- pages/functional/js/fizz-buzz.js | 50 +++++------ pages/functional/js/palindrome.js | 104 +++------------------- pages/functional/palindrome.html | 2 +- pages/oop/adapter/factorial.js | 17 ---- pages/oop/adapter/fibonacci.js | 21 ----- pages/oop/adapter/palindrome.js | 17 ---- pages/oop/factorial.html | 2 +- pages/oop/fibonacci.html | 2 +- pages/oop/fizz-buzz.html | 2 +- pages/oop/js/Factorial.js | 65 +++----------- pages/oop/js/Fibonacci.js | 81 ++++------------- pages/oop/js/Palindrome.js | 104 +++------------------- pages/oop/{adapter => js}/fizz-buzz.js | 54 +++++------ pages/oop/palindrome.html | 2 +- src/functional/factorial.js | 57 ++++++++++++ src/functional/fibonacci.js | 74 +++++++++++++++ src/functional/fizz-buzz.js | 31 +++++++ src/functional/palindrome.js | 97 ++++++++++++++++++++ src/oop/Factorial.js | 58 ++++++++++++ src/oop/Fibonacci.js | 70 +++++++++++++++ {pages/oop/js => src/oop}/FizzBuzz.js | 74 +++++++-------- src/oop/Palindrome.js | 97 ++++++++++++++++++++ pages/js/helper.js => src/utils/parser.js | 0 src/utils/validateApproach.js | 5 ++ src/utils/validateInput.js | 8 ++ src/utils/validateMethod.js | 11 +++ 45 files changed, 839 insertions(+), 895 deletions(-) delete mode 100644 bin/factorial/handleArgument.js delete mode 100644 bin/fibonacci/handleArgument.js delete mode 100644 bin/fizzbuzz/handleArgument.js delete mode 100644 bin/palindrome/handleArgument.js delete mode 100644 bin/shared/utils.js delete mode 100644 pages/functional/adapter/factorial.js delete mode 100644 pages/functional/adapter/fibonacci.js delete mode 100644 pages/functional/adapter/fizz-buzz.js delete mode 100644 pages/functional/adapter/palindrome.js delete mode 100644 pages/oop/adapter/factorial.js delete mode 100644 pages/oop/adapter/fibonacci.js delete mode 100644 pages/oop/adapter/palindrome.js rename pages/oop/{adapter => js}/fizz-buzz.js (90%) create mode 100644 src/functional/factorial.js create mode 100644 src/functional/fibonacci.js create mode 100644 src/functional/fizz-buzz.js create mode 100644 src/functional/palindrome.js create mode 100644 src/oop/Factorial.js create mode 100644 src/oop/Fibonacci.js rename {pages/oop/js => src/oop}/FizzBuzz.js (89%) create mode 100644 src/oop/Palindrome.js rename pages/js/helper.js => src/utils/parser.js (100%) create mode 100644 src/utils/validateApproach.js create mode 100644 src/utils/validateInput.js create mode 100644 src/utils/validateMethod.js diff --git a/bin/factorial/handleArgument.js b/bin/factorial/handleArgument.js deleted file mode 100644 index 62b6ece..0000000 --- a/bin/factorial/handleArgument.js +++ /dev/null @@ -1,31 +0,0 @@ -import { destructureArgument, validateArgument } from "../shared/utils.js"; - -export const handleArgument = cliArguments => { - let type = ""; - let method = ""; - let n = 0; - - for (let argument of cliArguments) { - if (!validateArgument(argument)) { - throw new Error(`invalid argument ${argument}`); - } - - const [key, value] = destructureArgument(argument); - - switch (key) { - case "--type": - type = value; - break; - case "--method": - method = value; - break; - case "--n": - n = value; - break; - default: - throw new Error(`invalid argument ${argument}`); - } - } - - return { type, method, n }; -}; diff --git a/bin/factorial/index.js b/bin/factorial/index.js index 5dad5c7..1378eef 100644 --- a/bin/factorial/index.js +++ b/bin/factorial/index.js @@ -1,33 +1,40 @@ -import { countFactorial } from "../../pages/functional/js/factorial.js"; +#!/usr/bin/env node -import { Factorial } from "../../pages/oop/js/Factorial.js"; +import readline from "readline"; +import { countFactorial } from "../../src/functional/factorial.js"; +import { Factorial } from "../../src/oop/Factorial.js"; +import { validateApproach } from "../../src/utils/validateApproach.js"; +import { validateFactorialMethod } from "../../src/utils/validateMethod.js"; +import { validateInputNumber } from "../../src/utils/validateInput.js"; -import { handleArgument } from "./handleArgument.js"; +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); -const cliArguments = process.argv.slice(2); -const { type, method, n } = handleArgument(cliArguments); -let result = 0; +rl.question("Select the factorial approach (functional / oop) : ", approach => { + validateApproach(approach); -if (!["oop", "functional"].includes(type)) { - throw new Error(`invalid type ${type}. must be "oop" or "functional"`); -} + rl.question("Select the factorial method (loop / recursive) : ", method => { + validateFactorialMethod(method); -if (!["loop", "recursive"].includes(method)) { - throw new Error(`invalid method ${method}. must be "loop" or "recursive"`); -} + rl.question("Input the number : ", number => { + validateInputNumber(number); -if (typeof parseInt(n) != "number") { - throw new Error(`invalid number ${n}. n should be a number"`); -} + let result = 0; -if (type == "functional") { - result = countFactorial(n, method); -} + if (approach == "functional") { + result = countFactorial(number, method); + } -if (type == "oop") { - const factorial = new Factorial(n); - result = factorial.count(method); -} + if (approach == "oop") { + result = new Factorial(number).count(method); + } -console.log(`------count factorial using ${method} (${type})------`); -console.log(`factorial of ${n} is ${result}`); + console.log(`------count factorial using ${method} (${approach})------`); + console.log(`factorial of ${number} is ${result}`); + + rl.close(); + }); + }); +}); diff --git a/bin/fibonacci/handleArgument.js b/bin/fibonacci/handleArgument.js deleted file mode 100644 index ab86854..0000000 --- a/bin/fibonacci/handleArgument.js +++ /dev/null @@ -1,31 +0,0 @@ -import { destructureArgument, validateArgument } from "../shared/utils.js"; - -export const handleArgument = cliArguments => { - let type = ""; - let method = ""; - let sequence = 0; - - for (let argument of cliArguments) { - if (!validateArgument(argument)) { - throw new Error(`invalid argument ${argument}`); - } - - const [key, value] = destructureArgument(argument); - - switch (key) { - case "--type": - type = value; - break; - case "--method": - method = value; - break; - case "--sequence": - sequence = value; - break; - default: - throw new Error(`invalid argument ${argument}`); - } - } - - return { type, method, sequence }; -}; diff --git a/bin/fibonacci/index.js b/bin/fibonacci/index.js index ee26084..fcb3019 100644 --- a/bin/fibonacci/index.js +++ b/bin/fibonacci/index.js @@ -1,33 +1,43 @@ -import { generateFibonacci } from "../../pages/functional/js/fibonacci.js"; -import { Fibonacci } from "../../pages/oop/js/Fibonacci.js"; +#!/usr/bin/env node -import { handleArgument } from "./handleArgument.js"; +import readline from "readline"; +import { generateFibonacci } from "../../src/functional/fibonacci.js"; +import { Fibonacci } from "../../src/oop/Fibonacci.js"; +import { validateApproach } from "../../src/utils/validateApproach.js"; +import { validateFibonacciMethod } from "../../src/utils/validateMethod.js"; +import { validateInputNumber } from "../../src/utils/validateInput.js"; -const cliArguments = process.argv.slice(2); -const { type, method, sequence } = handleArgument(cliArguments); -let result = 0; +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); -if (!["oop", "functional"].includes(type)) { - throw new Error(`invalid type ${type}. must be "oop" or "functional"`); -} +rl.question("Select the fibonacci approach (functional / oop) : ", approach => { + validateApproach(approach); -if (!["loop", "recursive"].includes(method)) { - throw new Error(`invalid method ${method}. must be "loop" or "recursive"`); -} + rl.question("Select the fibonacci method (loop / recursive) : ", method => { + validateFibonacciMethod(method); -if (typeof parseInt(sequence) != "number") { - throw new Error(`invalid number ${sequence}. n should be a number"`); -} + rl.question("Input the sequence : ", sequence => { + validateInputNumber(sequence); -if (type == "functional") { - result = generateFibonacci(sequence, method); -} + let result = []; -if (type == "oop") { - const fibonacci = new Fibonacci(sequence); - result = fibonacci.generate(method); -} + if (approach == "functional") { + result = generateFibonacci(sequence, method); + } -console.log(`------generate fibonacci using ${method} (${type})------`); -console.log(`fibonacci array with ${sequence} sequence :`); -console.log(result); + if (approach == "oop") { + result = new Fibonacci(sequence).generate(method); + } + + console.log( + `------generate fibonacci using ${method} (${approach})------`, + ); + console.log(`fibonacci array with ${sequence} sequence : `); + console.log(result); + + rl.close(); + }); + }); +}); diff --git a/bin/fizzbuzz/handleArgument.js b/bin/fizzbuzz/handleArgument.js deleted file mode 100644 index ab86854..0000000 --- a/bin/fizzbuzz/handleArgument.js +++ /dev/null @@ -1,31 +0,0 @@ -import { destructureArgument, validateArgument } from "../shared/utils.js"; - -export const handleArgument = cliArguments => { - let type = ""; - let method = ""; - let sequence = 0; - - for (let argument of cliArguments) { - if (!validateArgument(argument)) { - throw new Error(`invalid argument ${argument}`); - } - - const [key, value] = destructureArgument(argument); - - switch (key) { - case "--type": - type = value; - break; - case "--method": - method = value; - break; - case "--sequence": - sequence = value; - break; - default: - throw new Error(`invalid argument ${argument}`); - } - } - - return { type, method, sequence }; -}; diff --git a/bin/fizzbuzz/index.js b/bin/fizzbuzz/index.js index cd672d0..1b89b9e 100644 --- a/bin/fizzbuzz/index.js +++ b/bin/fizzbuzz/index.js @@ -1,28 +1,36 @@ -import { generateFizzBuzz } from "../../pages/functional/js/fizz-buzz.js"; -import { FizzBuzz } from "../../pages/oop/js/FizzBuzz.js"; -import { handleArgument } from "./handleArgument.js"; - -const cliArguments = process.argv.slice(2); -const { type, sequence } = handleArgument(cliArguments); -let result = 0; - -if (!["oop", "functional"].includes(type)) { - throw new Error(`invalid type ${type}. must be "oop" or "functional"`); -} - -if (typeof parseInt(sequence) != "number") { - throw new Error(`invalid number ${sequence}. n should be a number"`); -} - -if (type == "functional") { - result = generateFizzBuzz(sequence); -} - -if (type == "oop") { - const fizzbuzz = new FizzBuzz(sequence); - result = fizzbuzz.generate(); -} - -console.log(`------generate fizzbuzz (${type})------`); -console.log(`fizzbuzz array with ${sequence} sequence :`); -console.log(result); +#!/usr/bin/env node + +import readline from "readline"; +import { FizzBuzz } from "../../src/oop/FizzBuzz.js"; +import { generateFizzBuzz } from "../../src/functional/fizz-buzz.js"; +import { validateApproach } from "../../src/utils/validateApproach.js"; +import { validateInputNumber } from "../../src/utils/validateInput.js"; + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +rl.question("Select the fizz-buzz approach (functional / oop) : ", approach => { + validateApproach(approach); + + rl.question("Input the sequence : ", sequence => { + validateInputNumber(sequence); + + let result = 0; + + if (approach == "functional") { + result = generateFizzBuzz(sequence); + } + + if (approach == "oop") { + result = new FizzBuzz(sequence).generate(); + } + + console.log(`------generate fizzbuzz (${approach})------`); + console.log(`fizzbuzz array with ${sequence} sequence : `); + console.log(result); + + rl.close(); + }); +}); diff --git a/bin/palindrome/handleArgument.js b/bin/palindrome/handleArgument.js deleted file mode 100644 index 8228c65..0000000 --- a/bin/palindrome/handleArgument.js +++ /dev/null @@ -1,31 +0,0 @@ -import { destructureArgument, validateArgument } from "../shared/utils.js"; - -export const handleArgument = cliArguments => { - let type = ""; - let method = ""; - let word = ""; - - for (let argument of cliArguments) { - if (!validateArgument(argument)) { - throw new Error(`invalid argument ${argument}`); - } - - const [key, value] = destructureArgument(argument); - - switch (key) { - case "--type": - type = value; - break; - case "--method": - method = value; - break; - case "--word": - word = value; - break; - default: - throw new Error(`invalid argument ${argument}`); - } - } - - return { type, method, word }; -}; diff --git a/bin/palindrome/index.js b/bin/palindrome/index.js index 57c4f2d..1a5f39f 100644 --- a/bin/palindrome/index.js +++ b/bin/palindrome/index.js @@ -1,30 +1,46 @@ -import { generatePalindromeStatus } from "../../pages/functional/js/palindrome.js"; -import { Palindrome } from "../../pages/oop/js/Palindrome.js"; -import { handleArgument } from "./handleArgument.js"; - -const cliArguments = process.argv.slice(2); -const { type, method, word } = handleArgument(cliArguments); -let result = 0; - -if (!["oop", "functional"].includes(type)) { - throw new Error(`invalid type ${type}. must be "oop" or "functional"`); -} - -if (!["loop", "recursive", "reverse"].includes(method)) { - throw new Error( - `invalid method ${method}. must be "loop", "recursive" or "reverse"`, - ); -} - -if (type == "functional") { - result = generatePalindromeStatus(word, method); -} - -if (type == "oop") { - const palindrome = new Palindrome(word); - result = palindrome.generateStatus(method); -} - -console.log(`------generate palindrom status using ${method} (${type})------`); -console.log(`is ${word} a palindrome?`); -console.log(result); +#!/usr/bin/env node + +import readline from "readline"; +import { generatePalindromeStatus } from "../../src/functional/palindrome.js"; +import { Palindrome } from "../../src/oop/Palindrome.js"; +import { validateApproach } from "../../src/utils/validateApproach.js"; +import { validateFactorialMethod } from "../../src/utils/validateMethod.js"; + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +rl.question( + "Select the palindrome approach (functional / oop) : ", + approach => { + validateApproach(approach); + + rl.question( + "Select the palindrome method (loop / recursive) : ", + method => { + validateFactorialMethod(method); + + rl.question("Input the word : ", word => { + let result = 0; + + if (approach == "functional") { + result = generatePalindromeStatus(word, method); + } + + if (approach == "oop") { + result = new Palindrome(word).generateStatus(method); + } + + console.log( + `------generate palindrom status using ${method} (${approach})------`, + ); + console.log(`is ${word} a palindrome?`); + console.log(result); + + rl.close(); + }); + }, + ); + }, +); diff --git a/bin/shared/utils.js b/bin/shared/utils.js deleted file mode 100644 index 1ce9ccb..0000000 --- a/bin/shared/utils.js +++ /dev/null @@ -1,3 +0,0 @@ -export const destructureArgument = argument => argument.split("="); - -export const validateArgument = argument => /^--\w+=\w+$/.test(argument); diff --git a/package.json b/package.json index 8a1dd86..ea6e9c8 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,17 @@ "type": "module", "version": "1.0.0", "description": "Learn to code with version control system using Git.", + "bin": { + "factorial": "./bin/factorial/index.js", + "fibonacci": "./bin/fibonacci/index.js", + "fizzbuzz": "./bin/fizzbuzz/index.js", + "palindrome": "./bin/palindrome/index.js" + }, "scripts": { - "factorial": "node bin/factorial/index.js", - "fibonacci": "node bin/fibonacci/index.js", - "fizzbuzz": "node bin/fizzbuzz/index.js", - "palindrome": "node bin/palindrome/index.js" + "factorial": "node ./bin/factorial/index.js", + "fibonacci": "node ./bin/fibonacci/index.js", + "fizzbuzz": "node ./bin/fizzbuzz/index.js", + "palindrome": "node ./bin/palindrome/index.js" }, "repository": { "type": "git", diff --git a/pages/functional/adapter/factorial.js b/pages/functional/adapter/factorial.js deleted file mode 100644 index 7d50526..0000000 --- a/pages/functional/adapter/factorial.js +++ /dev/null @@ -1,17 +0,0 @@ -import { countFactorial } from "../js/factorial.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const n = event.target["n"].value; - const method = event.target["method"].value; - - const result = countFactorial(n, method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); diff --git a/pages/functional/adapter/fibonacci.js b/pages/functional/adapter/fibonacci.js deleted file mode 100644 index 7fe2bea..0000000 --- a/pages/functional/adapter/fibonacci.js +++ /dev/null @@ -1,19 +0,0 @@ -import { generateFibonacci } from "../js/fibonacci.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - const method = event.target["method"].value; - - const numbers = generateFibonacci(sequence, method); - - document.getElementById("result").innerHTML = numbers - .map(number => `
${number}
`) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); diff --git a/pages/functional/adapter/fizz-buzz.js b/pages/functional/adapter/fizz-buzz.js deleted file mode 100644 index 4648bdc..0000000 --- a/pages/functional/adapter/fizz-buzz.js +++ /dev/null @@ -1,27 +0,0 @@ -import { generateFizzBuzz } from "../js/fizz-buzz.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - - const fizzBuzzs = generateFizzBuzz(sequence); - - document.getElementById("result").innerHTML = fizzBuzzs - .map( - (fizzBuzz, index) => - `
${index + 1} ${fizzBuzz}
`, - ) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); diff --git a/pages/functional/adapter/palindrome.js b/pages/functional/adapter/palindrome.js deleted file mode 100644 index 3312671..0000000 --- a/pages/functional/adapter/palindrome.js +++ /dev/null @@ -1,17 +0,0 @@ -import { generatePalindromeStatus } from "../js/palindrome.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const word = event.target["word"].value; - const method = event.target["method"].value; - - const result = generatePalindromeStatus(word, method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); diff --git a/pages/functional/factorial.html b/pages/functional/factorial.html index a578709..7418a0a 100644 --- a/pages/functional/factorial.html +++ b/pages/functional/factorial.html @@ -63,6 +63,6 @@

Factorial (Functional)

Back to Homepage

- + diff --git a/pages/functional/fibonacci.html b/pages/functional/fibonacci.html index 7306e37..ddf598b 100644 --- a/pages/functional/fibonacci.html +++ b/pages/functional/fibonacci.html @@ -62,6 +62,6 @@

Fibonacci (Functional)

Back to Homepage

- + diff --git a/pages/functional/fizz-buzz.html b/pages/functional/fizz-buzz.html index ae009f2..176e66f 100644 --- a/pages/functional/fizz-buzz.html +++ b/pages/functional/fizz-buzz.html @@ -35,6 +35,6 @@

Fizz Buzz (Functional)

- + diff --git a/pages/functional/js/factorial.js b/pages/functional/js/factorial.js index ef2f196..e6753ba 100644 --- a/pages/functional/js/factorial.js +++ b/pages/functional/js/factorial.js @@ -1,57 +1,17 @@ -import { parseNumber } from "../../js/helper.js"; +import { countFactorial } from "../../../src/functional/factorial.js"; -/** - * Count factorial number from the given "n" value using loop way. - * - * @param {number} n - */ -function countFactorialUsingLoop(n) { - n = parseNumber(n); +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); - let result = 1; + try { + const n = event.target["n"].value; + const method = event.target["method"].value; - for (let index = n; index > 0; index--) { - result *= index; - } - - return result; -} + const result = countFactorial(n, method); -/** - * Count factorial number from the given "n" value using recursive way. - * - * @param {number} n - * @returns {number} - */ -function countFactorialUsingRecursive(n) { - n = parseNumber(n); - - if (n < 2) { - return 1; + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); } - - return n * countFactorialUsingRecursive(n - 1); -} - -/** - * Count factorial number from the given "n" value. - * - * @param {number} n - * @param {"loop" | "recursive"} method - * @throws {Error} - */ -function countFactorial(n, method) { - if (method == "loop") { - return countFactorialUsingLoop(n); - } else if (method == "recursive") { - return countFactorialUsingRecursive(n); - } else { - throw new Error("Method must be loop or recursive."); - } -} - -export { - countFactorialUsingLoop, - countFactorialUsingRecursive, - countFactorial, -}; +}); diff --git a/pages/functional/js/fibonacci.js b/pages/functional/js/fibonacci.js index a532d9e..f79cb5f 100644 --- a/pages/functional/js/fibonacci.js +++ b/pages/functional/js/fibonacci.js @@ -1,74 +1,19 @@ -import { parseNumber } from "../../js/helper.js"; +import { generateFibonacci } from "../../../src/functional/fibonacci.js"; -/** - * Create an array filled by fibonacci sequence using loop way. - * - * @param {number} sequence - */ -function generateFibonacciUsingLoop(sequence) { - sequence = parseNumber(sequence); +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); - let result = []; - let prev = 0; - let next = 1; + try { + const sequence = event.target["sequence"].value; + const method = event.target["method"].value; - for (let index = 0; index < sequence; index++) { - result.push(prev); + const numbers = generateFibonacci(sequence, method); - const current = prev + next; - prev = next; - next = current; + document.getElementById("result").innerHTML = numbers + .map(number => `
${number}
`) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); } - - return result; -} - -/** - * Create an array filled by fibonacci sequence using recursive way. - * - * @param {number} sequence - */ -function generateFibonacciUsingRecursive(sequence) { - /** - * @param {number} n - * @returns {number} - */ - function fibonacci(n) { - return n < 1 ? 0 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2); - } - - sequence = parseNumber(sequence); - - let result = []; - - for (let index = 0; index < sequence; index++) { - const current = fibonacci(index); - - result.push(current); - } - - return result; -} - -/** - * Create an array filled by fibonacci sequence. - * - * @param {number} sequence - * @param {"loop"| "recursive"} method - * @throws {Error} - */ -function generateFibonacci(sequence, method) { - if (method === "loop") { - return generateFibonacciUsingLoop(sequence); - } else if (method === "recursive") { - return generateFibonacciUsingRecursive(sequence); - } else { - throw new Error("Method must be loop or recursive."); - } -} - -export { - generateFibonacciUsingLoop, - generateFibonacciUsingRecursive, - generateFibonacci, -}; +}); diff --git a/pages/functional/js/fizz-buzz.js b/pages/functional/js/fizz-buzz.js index 9c39e2d..1c3065f 100644 --- a/pages/functional/js/fizz-buzz.js +++ b/pages/functional/js/fizz-buzz.js @@ -1,31 +1,27 @@ -import { parseNumber } from "../../js/helper.js"; +import { generateFizzBuzz } from "../../../src/functional/fizz-buzz.js"; -/** - * Create an array filled by "fizz", "buzz", or "fizz buzz" based on - * this requirement below (highest priority from above). - * - * Muliples of 4 or 7: "fizz buzz" - * Odd sequence: "fizz" - * Even sequence: "buzz" - * - * @param {number} sequence - */ -function generateFizzBuzz(sequence) { - sequence = parseNumber(sequence); +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); - let result = []; + try { + const sequence = event.target["sequence"].value; - for (let index = 1; index <= sequence; index++) { - if (index % 4 === 0 || index % 7 === 0) { - result.push("fizz buzz"); - } else if (index % 2 === 1) { - result.push("fizz"); - } else if (index % 2 === 0) { - result.push("buzz"); - } - } - - return result; -} + const fizzBuzzs = generateFizzBuzz(sequence); -export { generateFizzBuzz }; + document.getElementById("result").innerHTML = fizzBuzzs + .map( + (fizzBuzz, index) => + `
${index + 1} ${fizzBuzz}
`, + ) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/functional/js/palindrome.js b/pages/functional/js/palindrome.js index 4baa972..e1b871a 100644 --- a/pages/functional/js/palindrome.js +++ b/pages/functional/js/palindrome.js @@ -1,97 +1,17 @@ -import { parseString } from "../../js/helper.js"; +import { generatePalindromeStatus } from "../../../src/functional/palindrome.js"; -/** - * Determine whether the given value is a palindrome or not using reverse way. - * - * @param {string} value - */ -function isPalindromeUsingReverse(value) { - value = parseString(value); +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); - let newValue = ""; + try { + const word = event.target["word"].value; + const method = event.target["method"].value; - for (let index = value.length - 1; index >= 0; index--) { - newValue += value[index]; - } - - return value === newValue; -} - -/** - * Determine whether the given value is a palindrome or not using loop way. - * - * @param {string} value - */ -function isPalindromeUsingLoop(value) { - value = parseString(value); - - for (let index = 0; index < Math.floor(value.length / 2); index++) { - const lastCharacterIndex = value.length - (index + 1); - - const firstCharacter = value[index]; - const lastCharacter = value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } - } - - return true; -} - -/** - * Determine whether the given value is a palindrome or not using recursive way. - * - * @param {string} value - * @param {number} index - */ -function isPalindromeUsingRecursive(value, index = 0) { - value = parseString(value); + const result = generatePalindromeStatus(word, method); - if (index < Math.floor(value.length / 2)) { - const lastCharacterIndex = value.length - (index + 1); - - const firstCharacter = value[index]; - const lastCharacter = value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } - - return isPalindromeUsingRecursive(value, index + 1); + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); } - - return true; -} - -/** - * Return palindrome human-readable description. - * - * @param {string} word - * @param {"reverse" | "loop" | "recursive"} method - * @throws {Error} - */ -function generatePalindromeStatus(word, method) { - let isPalindrome; - - if (method === "reverse") { - isPalindrome = isPalindromeUsingReverse(word); - } else if (method === "loop") { - isPalindrome = isPalindromeUsingLoop(word); - } else if (method === "recursive") { - isPalindrome = isPalindromeUsingRecursive(word); - } else { - throw new Error("Method must be reverse, loop, or recursive."); - } - - return isPalindrome - ? "Yes, this word is a palindrome." - : "No, this word is not a palindrome."; -} - -export { - isPalindromeUsingLoop, - isPalindromeUsingRecursive, - isPalindromeUsingReverse, - generatePalindromeStatus, -}; +}); diff --git a/pages/functional/palindrome.html b/pages/functional/palindrome.html index d3e1ca1..a1b48fc 100644 --- a/pages/functional/palindrome.html +++ b/pages/functional/palindrome.html @@ -65,6 +65,6 @@

Palindrome (Functional)

Back to Homepage

- + diff --git a/pages/oop/adapter/factorial.js b/pages/oop/adapter/factorial.js deleted file mode 100644 index 647f23d..0000000 --- a/pages/oop/adapter/factorial.js +++ /dev/null @@ -1,17 +0,0 @@ -import { Factorial } from "../js/Factorial.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const n = event.target["n"].value; - const method = event.target["method"].value; - - const result = new Factorial(n).count(method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); diff --git a/pages/oop/adapter/fibonacci.js b/pages/oop/adapter/fibonacci.js deleted file mode 100644 index 4eae253..0000000 --- a/pages/oop/adapter/fibonacci.js +++ /dev/null @@ -1,21 +0,0 @@ -import { Fibonacci } from "../js/fibonacci.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - const method = event.target["method"].value; - - const numbers = new Fibonacci(sequence).generate(method); - - document.getElementById("result").innerHTML = document.getElementById( - "result", - ).innerHTML = numbers - .map(number => `
${number}
`) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); diff --git a/pages/oop/adapter/palindrome.js b/pages/oop/adapter/palindrome.js deleted file mode 100644 index 763deaa..0000000 --- a/pages/oop/adapter/palindrome.js +++ /dev/null @@ -1,17 +0,0 @@ -import { Palindrome } from "../js/palindrome.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const word = event.target["word"].value; - const method = event.target["method"].value; - - const result = new Palindrome(word).generateStatus(method); - - document.getElementById("result").textContent = result; - } catch (error) { - alert(error.message); - console.error(error); - } -}); diff --git a/pages/oop/factorial.html b/pages/oop/factorial.html index 35d9686..776893e 100644 --- a/pages/oop/factorial.html +++ b/pages/oop/factorial.html @@ -63,6 +63,6 @@

Factorial (OOP)

Back to Homepage

- + diff --git a/pages/oop/fibonacci.html b/pages/oop/fibonacci.html index 27eca19..9c38561 100644 --- a/pages/oop/fibonacci.html +++ b/pages/oop/fibonacci.html @@ -62,6 +62,6 @@

Fibonacci (OOP)

Back to Homepage

- + diff --git a/pages/oop/fizz-buzz.html b/pages/oop/fizz-buzz.html index 52265fa..6d3db0a 100644 --- a/pages/oop/fizz-buzz.html +++ b/pages/oop/fizz-buzz.html @@ -35,6 +35,6 @@

Fizz Buzz (OOP)

- + diff --git a/pages/oop/js/Factorial.js b/pages/oop/js/Factorial.js index d4fd5db..c61fff2 100644 --- a/pages/oop/js/Factorial.js +++ b/pages/oop/js/Factorial.js @@ -1,58 +1,17 @@ -import { parseNumber } from "../../js/helper.js"; +import { Factorial } from "../../../src/oop/Factorial.js"; -export class Factorial { - /** @type {number} */ - n; +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); - /** - * @param {number} n - */ - constructor(n) { - this.n = parseNumber(n); - } - - /** - * Count factorial number from the given "n" value using loop way. - */ - countUsingLoop() { - let result = 1; - - for (let index = this.n; index > 0; index--) { - result = result * index; - } - - return result; - } - - /** - * Count factorial number from the given "n" value using recursive way. - * - * @param {number | undefined} n - * @returns {number} - */ - countUsingRecursive(n = undefined) { - n ||= this.n; + try { + const n = event.target["n"].value; + const method = event.target["method"].value; - if (n < 2) { - return 1; - } - - return n * this.countUsingRecursive(n - 1); - } + const result = new Factorial(n).count(method); - /** - * Count factorial number from the given "n" value. - * - * @param {"loop" | "recursive"} method - * @throws {Error} - */ - count(method) { - if (method == "loop") { - return this.countUsingLoop(); - } else if (method == "recursive") { - return this.countUsingRecursive(); - } else { - throw new Error("Method must be loop or recursive."); - } + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); } -} +}); diff --git a/pages/oop/js/Fibonacci.js b/pages/oop/js/Fibonacci.js index 9235c7d..616ca23 100644 --- a/pages/oop/js/Fibonacci.js +++ b/pages/oop/js/Fibonacci.js @@ -1,70 +1,21 @@ -import { parseNumber } from "../../js/helper.js"; +import { Fibonacci } from "../../../src/oop/Fibonacci.js"; -export class Fibonacci { - /** @type {number} */ - sequence; +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); - /** - * @param {number} sequence - */ - constructor(sequence) { - this.sequence = parseNumber(sequence); - } - - /** - * @param {number} n - * @returns {number} - */ - static get(n) { - return n < 1 ? 0 : n <= 2 ? 1 : this.get(n - 1) + this.get(n - 2); - } - - /** - * Create an array filled by fibonacci sequence using loop way. - */ - generateUsingLoop() { - let result = []; - let prev = 0; - let next = 1; - - for (let index = 0; index < this.sequence; index++) { - result.push(prev); - - const current = prev + next; - prev = next; - next = current; - } + try { + const sequence = event.target["sequence"].value; + const method = event.target["method"].value; - return result; - } - - /** - * Create an array filled by fibonacci sequence using recursive way. - */ - generateUsingRecursive() { - let result = []; - - for (let index = 0; index < this.sequence; index++) { - const current = Fibonacci.get(index); - - result.push(current); - } - - return result; - } + const numbers = new Fibonacci(sequence).generate(method); - /** - * Create an array filled by fibonacci sequence. - * - * @param {"loop" | "recursive"} method - */ - generate(method) { - if (method === "loop") { - return this.generateUsingLoop(); - } else if (method === "recursive") { - return this.generateUsingRecursive(); - } else { - throw new Error("Method must be loop or recursive."); - } + document.getElementById("result").innerHTML = document.getElementById( + "result", + ).innerHTML = numbers + .map(number => `
${number}
`) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); } -} +}); diff --git a/pages/oop/js/Palindrome.js b/pages/oop/js/Palindrome.js index 2c0cc8f..ff95c27 100644 --- a/pages/oop/js/Palindrome.js +++ b/pages/oop/js/Palindrome.js @@ -1,97 +1,17 @@ -import { parseString } from "../../js/helper.js"; +import { Palindrome } from "../../../src/oop/Palindrome.js"; -export class Palindrome { - /** @type {string} */ - value; +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); - /** - * @param {string} value - */ - constructor(value) { - this.value = parseString(value); - } - - /** - * Determine whether the given value is a palindrome or not using reverse way. - */ - evaluateUsingReverse() { - let newValue = ""; - - for (let index = this.value.length - 1; index >= 0; index--) { - newValue += this.value[index]; - } - - return this.value === newValue; - } - - /** - * Determine whether the given value is a palindrome or not using loop way. - */ - evaluateUsingLoop() { - for (let index = 0; index < Math.floor(this.value.length / 2); index++) { - const lastCharacterIndex = this.value.length - (index + 1); - - const firstCharacter = this.value[index]; - const lastCharacter = this.value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } - } - - return true; - } - - /** - * Determine whether the given value is a palindrome or not using recursive way. - * - * @param {number | undefined} index - */ - evaluateUsingRecursive(index = undefined) { - index ||= 0; - - if (index < Math.floor(this.value.length / 2)) { - const lastCharacterIndex = this.value.length - (index + 1); - - const firstCharacter = this.value[index]; - const lastCharacter = this.value[lastCharacterIndex]; - - if (firstCharacter !== lastCharacter) { - return false; - } + try { + const word = event.target["word"].value; + const method = event.target["method"].value; - return this.evaluateUsingRecursive(this.value, index + 1); - } - - return true; - } - - /** - * Determine whether the given value is a palindrome or not. - * - * @typedef {"reverse" | "loop" | "recursive"} Method - * @param {Method} method - */ - evaluate(method) { - if (method === "reverse") { - return this.evaluateUsingReverse(); - } else if (method === "loop") { - return this.evaluateUsingLoop(); - } else if (method === "recursive") { - return this.evaluateUsingRecursive(); - } else { - throw new Error("Method must be reverse, loop, or recursive."); - } - } + const result = new Palindrome(word).generateStatus(method); - /** - * Return palindrome human-readable description. - * - * @param {Method} method - */ - generateStatus(method) { - return this.evaluate(method) - ? "Yes, this word is a palindrome." - : "No, this word is not a palindrome."; + document.getElementById("result").textContent = result; + } catch (error) { + alert(error.message); + console.error(error); } -} +}); diff --git a/pages/oop/adapter/fizz-buzz.js b/pages/oop/js/fizz-buzz.js similarity index 90% rename from pages/oop/adapter/fizz-buzz.js rename to pages/oop/js/fizz-buzz.js index d4e5be2..cea5b4b 100644 --- a/pages/oop/adapter/fizz-buzz.js +++ b/pages/oop/js/fizz-buzz.js @@ -1,27 +1,27 @@ -import { FizzBuzz } from "../js/FizzBuzz.js"; - -document.getElementById("form").addEventListener("submit", function (event) { - event.preventDefault(); - - try { - const sequence = event.target["sequence"].value; - - const fizzBuzzs = new FizzBuzz(sequence).generate(); - - document.getElementById("result").innerHTML = fizzBuzzs - .map( - (fizzBuzz, index) => - `
${index + 1} ${fizzBuzz}
`, - ) - .join(""); - } catch (error) { - alert(error.message); - console.error(error); - } -}); +import { FizzBuzz } from "../../../src/oop/FizzBuzz.js"; + +document.getElementById("form").addEventListener("submit", function (event) { + event.preventDefault(); + + try { + const sequence = event.target["sequence"].value; + + const fizzBuzzs = new FizzBuzz(sequence).generate(); + + document.getElementById("result").innerHTML = fizzBuzzs + .map( + (fizzBuzz, index) => + `
${index + 1} ${fizzBuzz}
`, + ) + .join(""); + } catch (error) { + alert(error.message); + console.error(error); + } +}); diff --git a/pages/oop/palindrome.html b/pages/oop/palindrome.html index 3ae1368..0e9fb8b 100644 --- a/pages/oop/palindrome.html +++ b/pages/oop/palindrome.html @@ -65,6 +65,6 @@

Palindrome (OOP)

Back to Homepage

- + diff --git a/src/functional/factorial.js b/src/functional/factorial.js new file mode 100644 index 0000000..ab14418 --- /dev/null +++ b/src/functional/factorial.js @@ -0,0 +1,57 @@ +import { parseNumber } from "../utils/parser.js"; + +/** + * Count factorial number from the given "n" value using loop way. + * + * @param {number} n + */ +function countFactorialUsingLoop(n) { + n = parseNumber(n); + + let result = 1; + + for (let index = n; index > 0; index--) { + result *= index; + } + + return result; +} + +/** + * Count factorial number from the given "n" value using recursive way. + * + * @param {number} n + * @returns {number} + */ +function countFactorialUsingRecursive(n) { + n = parseNumber(n); + + if (n < 2) { + return 1; + } + + return n * countFactorialUsingRecursive(n - 1); +} + +/** + * Count factorial number from the given "n" value. + * + * @param {number} n + * @param {"loop" | "recursive"} method + * @throws {Error} + */ +function countFactorial(n, method) { + if (method == "loop") { + return countFactorialUsingLoop(n); + } else if (method == "recursive") { + return countFactorialUsingRecursive(n); + } else { + throw new Error("Method must be loop or recursive."); + } +} + +export { + countFactorialUsingLoop, + countFactorialUsingRecursive, + countFactorial, +}; diff --git a/src/functional/fibonacci.js b/src/functional/fibonacci.js new file mode 100644 index 0000000..bf3363d --- /dev/null +++ b/src/functional/fibonacci.js @@ -0,0 +1,74 @@ +import { parseNumber } from "../utils/parser.js"; + +/** + * Create an array filled by fibonacci sequence using loop way. + * + * @param {number} sequence + */ +function generateFibonacciUsingLoop(sequence) { + sequence = parseNumber(sequence); + + let result = []; + let prev = 0; + let next = 1; + + for (let index = 0; index < sequence; index++) { + result.push(prev); + + const current = prev + next; + prev = next; + next = current; + } + + return result; +} + +/** + * Create an array filled by fibonacci sequence using recursive way. + * + * @param {number} sequence + */ +function generateFibonacciUsingRecursive(sequence) { + /** + * @param {number} n + * @returns {number} + */ + function fibonacci(n) { + return n < 1 ? 0 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2); + } + + sequence = parseNumber(sequence); + + let result = []; + + for (let index = 0; index < sequence; index++) { + const current = fibonacci(index); + + result.push(current); + } + + return result; +} + +/** + * Create an array filled by fibonacci sequence. + * + * @param {number} sequence + * @param {"loop"| "recursive"} method + * @throws {Error} + */ +function generateFibonacci(sequence, method) { + if (method === "loop") { + return generateFibonacciUsingLoop(sequence); + } else if (method === "recursive") { + return generateFibonacciUsingRecursive(sequence); + } else { + throw new Error("Method must be loop or recursive."); + } +} + +export { + generateFibonacciUsingLoop, + generateFibonacciUsingRecursive, + generateFibonacci, +}; diff --git a/src/functional/fizz-buzz.js b/src/functional/fizz-buzz.js new file mode 100644 index 0000000..e64bf10 --- /dev/null +++ b/src/functional/fizz-buzz.js @@ -0,0 +1,31 @@ +import { parseNumber } from "../utils/parser.js"; + +/** + * Create an array filled by "fizz", "buzz", or "fizz buzz" based on + * this requirement below (highest priority from above). + * + * Muliples of 4 or 7: "fizz buzz" + * Odd sequence: "fizz" + * Even sequence: "buzz" + * + * @param {number} sequence + */ +function generateFizzBuzz(sequence) { + sequence = parseNumber(sequence); + + let result = []; + + for (let index = 1; index <= sequence; index++) { + if (index % 4 === 0 || index % 7 === 0) { + result.push("fizz buzz"); + } else if (index % 2 === 1) { + result.push("fizz"); + } else if (index % 2 === 0) { + result.push("buzz"); + } + } + + return result; +} + +export { generateFizzBuzz }; diff --git a/src/functional/palindrome.js b/src/functional/palindrome.js new file mode 100644 index 0000000..9ad6cee --- /dev/null +++ b/src/functional/palindrome.js @@ -0,0 +1,97 @@ +import { parseString } from "../utils/parser.js"; + +/** + * Determine whether the given value is a palindrome or not using reverse way. + * + * @param {string} value + */ +function isPalindromeUsingReverse(value) { + value = parseString(value); + + let newValue = ""; + + for (let index = value.length - 1; index >= 0; index--) { + newValue += value[index]; + } + + return value === newValue; +} + +/** + * Determine whether the given value is a palindrome or not using loop way. + * + * @param {string} value + */ +function isPalindromeUsingLoop(value) { + value = parseString(value); + + for (let index = 0; index < Math.floor(value.length / 2); index++) { + const lastCharacterIndex = value.length - (index + 1); + + const firstCharacter = value[index]; + const lastCharacter = value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + } + + return true; +} + +/** + * Determine whether the given value is a palindrome or not using recursive way. + * + * @param {string} value + * @param {number} index + */ +function isPalindromeUsingRecursive(value, index = 0) { + value = parseString(value); + + if (index < Math.floor(value.length / 2)) { + const lastCharacterIndex = value.length - (index + 1); + + const firstCharacter = value[index]; + const lastCharacter = value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + + return isPalindromeUsingRecursive(value, index + 1); + } + + return true; +} + +/** + * Return palindrome human-readable description. + * + * @param {string} word + * @param {"reverse" | "loop" | "recursive"} method + * @throws {Error} + */ +function generatePalindromeStatus(word, method) { + let isPalindrome; + + if (method === "reverse") { + isPalindrome = isPalindromeUsingReverse(word); + } else if (method === "loop") { + isPalindrome = isPalindromeUsingLoop(word); + } else if (method === "recursive") { + isPalindrome = isPalindromeUsingRecursive(word); + } else { + throw new Error("Method must be reverse, loop, or recursive."); + } + + return isPalindrome + ? "Yes, this word is a palindrome." + : "No, this word is not a palindrome."; +} + +export { + isPalindromeUsingLoop, + isPalindromeUsingRecursive, + isPalindromeUsingReverse, + generatePalindromeStatus, +}; diff --git a/src/oop/Factorial.js b/src/oop/Factorial.js new file mode 100644 index 0000000..21b5a1e --- /dev/null +++ b/src/oop/Factorial.js @@ -0,0 +1,58 @@ +import { parseNumber } from "../utils/parser.js"; + +export class Factorial { + /** @type {number} */ + n; + + /** + * @param {number} n + */ + constructor(n) { + this.n = parseNumber(n); + } + + /** + * Count factorial number from the given "n" value using loop way. + */ + countUsingLoop() { + let result = 1; + + for (let index = this.n; index > 0; index--) { + result = result * index; + } + + return result; + } + + /** + * Count factorial number from the given "n" value using recursive way. + * + * @param {number | undefined} n + * @returns {number} + */ + countUsingRecursive(n = undefined) { + n ||= this.n; + + if (n < 2) { + return 1; + } + + return n * this.countUsingRecursive(n - 1); + } + + /** + * Count factorial number from the given "n" value. + * + * @param {"loop" | "recursive"} method + * @throws {Error} + */ + count(method) { + if (method == "loop") { + return this.countUsingLoop(); + } else if (method == "recursive") { + return this.countUsingRecursive(); + } else { + throw new Error("Method must be loop or recursive."); + } + } +} diff --git a/src/oop/Fibonacci.js b/src/oop/Fibonacci.js new file mode 100644 index 0000000..abcaacc --- /dev/null +++ b/src/oop/Fibonacci.js @@ -0,0 +1,70 @@ +import { parseNumber } from "../utils/parser.js"; + +export class Fibonacci { + /** @type {number} */ + sequence; + + /** + * @param {number} sequence + */ + constructor(sequence) { + this.sequence = parseNumber(sequence); + } + + /** + * @param {number} n + * @returns {number} + */ + static get(n) { + return n < 1 ? 0 : n <= 2 ? 1 : this.get(n - 1) + this.get(n - 2); + } + + /** + * Create an array filled by fibonacci sequence using loop way. + */ + generateUsingLoop() { + let result = []; + let prev = 0; + let next = 1; + + for (let index = 0; index < this.sequence; index++) { + result.push(prev); + + const current = prev + next; + prev = next; + next = current; + } + + return result; + } + + /** + * Create an array filled by fibonacci sequence using recursive way. + */ + generateUsingRecursive() { + let result = []; + + for (let index = 0; index < this.sequence; index++) { + const current = Fibonacci.get(index); + + result.push(current); + } + + return result; + } + + /** + * Create an array filled by fibonacci sequence. + * + * @param {"loop" | "recursive"} method + */ + generate(method) { + if (method === "loop") { + return this.generateUsingLoop(); + } else if (method === "recursive") { + return this.generateUsingRecursive(); + } else { + throw new Error("Method must be loop or recursive."); + } + } +} diff --git a/pages/oop/js/FizzBuzz.js b/src/oop/FizzBuzz.js similarity index 89% rename from pages/oop/js/FizzBuzz.js rename to src/oop/FizzBuzz.js index 7cea3af..92a907f 100644 --- a/pages/oop/js/FizzBuzz.js +++ b/src/oop/FizzBuzz.js @@ -1,37 +1,37 @@ -import { parseNumber } from "../../js/helper.js"; - -export class FizzBuzz { - /** @type {number} */ - sequence; - - /** - * @param {number} sequence - */ - constructor(sequence) { - this.sequence = parseNumber(sequence); - } - - /** - * Create an array filled by "fizz", "buzz", or "fizz buzz" based on - * this requirement below (highest priority from above). - * - * Muliples of 4 or 7: "fizz buzz" - * Odd sequence: "fizz" - * Even sequence: "buzz" - */ - generate() { - let result = []; - - for (let index = 1; index <= this.sequence; index++) { - if (index % 4 === 0 || index % 7 === 0) { - result.push("fizz buzz"); - } else if (index % 2 === 1) { - result.push("fizz"); - } else if (index % 2 === 0) { - result.push("buzz"); - } - } - - return result; - } -} +import { parseNumber } from "../utils/parser.js"; + +export class FizzBuzz { + /** @type {number} */ + sequence; + + /** + * @param {number} sequence + */ + constructor(sequence) { + this.sequence = parseNumber(sequence); + } + + /** + * Create an array filled by "fizz", "buzz", or "fizz buzz" based on + * this requirement below (highest priority from above). + * + * Muliples of 4 or 7: "fizz buzz" + * Odd sequence: "fizz" + * Even sequence: "buzz" + */ + generate() { + let result = []; + + for (let index = 1; index <= this.sequence; index++) { + if (index % 4 === 0 || index % 7 === 0) { + result.push("fizz buzz"); + } else if (index % 2 === 1) { + result.push("fizz"); + } else if (index % 2 === 0) { + result.push("buzz"); + } + } + + return result; + } +} diff --git a/src/oop/Palindrome.js b/src/oop/Palindrome.js new file mode 100644 index 0000000..26204bd --- /dev/null +++ b/src/oop/Palindrome.js @@ -0,0 +1,97 @@ +import { parseString } from "../utils/parser.js"; + +export class Palindrome { + /** @type {string} */ + value; + + /** + * @param {string} value + */ + constructor(value) { + this.value = parseString(value); + } + + /** + * Determine whether the given value is a palindrome or not using reverse way. + */ + evaluateUsingReverse() { + let newValue = ""; + + for (let index = this.value.length - 1; index >= 0; index--) { + newValue += this.value[index]; + } + + return this.value === newValue; + } + + /** + * Determine whether the given value is a palindrome or not using loop way. + */ + evaluateUsingLoop() { + for (let index = 0; index < Math.floor(this.value.length / 2); index++) { + const lastCharacterIndex = this.value.length - (index + 1); + + const firstCharacter = this.value[index]; + const lastCharacter = this.value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + } + + return true; + } + + /** + * Determine whether the given value is a palindrome or not using recursive way. + * + * @param {number | undefined} index + */ + evaluateUsingRecursive(index = undefined) { + index ||= 0; + + if (index < Math.floor(this.value.length / 2)) { + const lastCharacterIndex = this.value.length - (index + 1); + + const firstCharacter = this.value[index]; + const lastCharacter = this.value[lastCharacterIndex]; + + if (firstCharacter !== lastCharacter) { + return false; + } + + return this.evaluateUsingRecursive(this.value, index + 1); + } + + return true; + } + + /** + * Determine whether the given value is a palindrome or not. + * + * @typedef {"reverse" | "loop" | "recursive"} Method + * @param {Method} method + */ + evaluate(method) { + if (method === "reverse") { + return this.evaluateUsingReverse(); + } else if (method === "loop") { + return this.evaluateUsingLoop(); + } else if (method === "recursive") { + return this.evaluateUsingRecursive(); + } else { + throw new Error("Method must be reverse, loop, or recursive."); + } + } + + /** + * Return palindrome human-readable description. + * + * @param {Method} method + */ + generateStatus(method) { + return this.evaluate(method) + ? "Yes, this word is a palindrome." + : "No, this word is not a palindrome."; + } +} diff --git a/pages/js/helper.js b/src/utils/parser.js similarity index 100% rename from pages/js/helper.js rename to src/utils/parser.js diff --git a/src/utils/validateApproach.js b/src/utils/validateApproach.js new file mode 100644 index 0000000..b859c9a --- /dev/null +++ b/src/utils/validateApproach.js @@ -0,0 +1,5 @@ +export const validateApproach = approach => { + if (!["factorial", "oop"].includes(approach)) { + throw TypeError("only 'functional' or 'oop' approach are allowed!"); + } +}; diff --git a/src/utils/validateInput.js b/src/utils/validateInput.js new file mode 100644 index 0000000..047cef2 --- /dev/null +++ b/src/utils/validateInput.js @@ -0,0 +1,8 @@ +import { parseNumber, parseString } from "./parser.js"; + +export const validateInputNumber = input => { + let parsedInput = parseNumber(input); + if (typeof parsedInput != "number") { + throw TypeError("only numbers are allowed as input"); + } +}; diff --git a/src/utils/validateMethod.js b/src/utils/validateMethod.js new file mode 100644 index 0000000..e3d125c --- /dev/null +++ b/src/utils/validateMethod.js @@ -0,0 +1,11 @@ +export const validateFactorialMethod = method => { + if (!["loop", "recursive"].includes(method)) { + throw new TypeError("only 'loop' or 'recursive' method are allowed!"); + } +}; + +export const validateFibonacciMethod = method => { + if (!["loop", "recursive"].includes(method)) { + throw new TypeError("only 'loop' or 'recursive' method are allowed!"); + } +}; From 53040c4a8b40524ee03b461524c57a5e9f250234 Mon Sep 17 00:00:00 2001 From: dianprsty Date: Sat, 4 Nov 2023 06:23:56 +0700 Subject: [PATCH 4/4] fix : fix wrong validation --- bin/palindrome/index.js | 9 ++++++--- src/utils/validateApproach.js | 2 +- src/utils/validateInput.js | 2 +- src/utils/validateMethod.js | 8 ++++++++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/bin/palindrome/index.js b/bin/palindrome/index.js index 1a5f39f..116164c 100644 --- a/bin/palindrome/index.js +++ b/bin/palindrome/index.js @@ -4,7 +4,10 @@ import readline from "readline"; import { generatePalindromeStatus } from "../../src/functional/palindrome.js"; import { Palindrome } from "../../src/oop/Palindrome.js"; import { validateApproach } from "../../src/utils/validateApproach.js"; -import { validateFactorialMethod } from "../../src/utils/validateMethod.js"; +import { + validateFactorialMethod, + validatePalindromeMethod, +} from "../../src/utils/validateMethod.js"; const rl = readline.createInterface({ input: process.stdin, @@ -17,9 +20,9 @@ rl.question( validateApproach(approach); rl.question( - "Select the palindrome method (loop / recursive) : ", + "Select the palindrome method (reverse / loop / recursive) : ", method => { - validateFactorialMethod(method); + validatePalindromeMethod(method); rl.question("Input the word : ", word => { let result = 0; diff --git a/src/utils/validateApproach.js b/src/utils/validateApproach.js index b859c9a..f012f4d 100644 --- a/src/utils/validateApproach.js +++ b/src/utils/validateApproach.js @@ -1,5 +1,5 @@ export const validateApproach = approach => { - if (!["factorial", "oop"].includes(approach)) { + if (!["functional", "oop"].includes(approach)) { throw TypeError("only 'functional' or 'oop' approach are allowed!"); } }; diff --git a/src/utils/validateInput.js b/src/utils/validateInput.js index 047cef2..6be1b4a 100644 --- a/src/utils/validateInput.js +++ b/src/utils/validateInput.js @@ -1,4 +1,4 @@ -import { parseNumber, parseString } from "./parser.js"; +import { parseNumber } from "./parser.js"; export const validateInputNumber = input => { let parsedInput = parseNumber(input); diff --git a/src/utils/validateMethod.js b/src/utils/validateMethod.js index e3d125c..c4dd0f9 100644 --- a/src/utils/validateMethod.js +++ b/src/utils/validateMethod.js @@ -9,3 +9,11 @@ export const validateFibonacciMethod = method => { throw new TypeError("only 'loop' or 'recursive' method are allowed!"); } }; + +export const validatePalindromeMethod = method => { + if (!["reverse", "loop", "recursive"].includes(method)) { + throw new TypeError( + "only 'reverse', 'loop' or 'recursive' method are allowed!", + ); + } +};