diff --git a/README.md b/README.md index 5cea6fd..60dd4c7 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@ Learn to code with version control system using Git. There are several study case we can use to expertise our skill on Git, such as: - [x] [Factorial - Functional](functional/factorial.html) -- [ ] [Factorial - OOP](oop/factorial.html) +- [x] [Factorial - OOP](oop/factorial.html) - [x] [Fizz Buzz - Functional](functional/fizz-buzz.html) - [x] [Fizz Buzz - OOP](oop/fizz-buzz.html) -- [ ] [Palindrome - Functional](functional/palindrome.html) -- [ ] [Palindrome - OOP](oop/palindrome.html) -- [ ] [Fibonacci - Functional](functional/fibonacci.html) -- [ ] [Fibonacci - OOP](oop/fibonacci.html) +- [x] [Palindrome - Functional](functional/palindrome.html) +- [x] [Palindrome - OOP](oop/palindrome.html) +- [x] [Fibonacci - Functional](functional/fibonacci.html) +- [x] [Fibonacci - OOP](oop/fibonacci.html) - [x] [JavaScript Modules](javascript-modules/index.html) You can add your own study case into the list above by contributing to this repository. See [Contributing](#contributing) section for more information. @@ -33,6 +33,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ## Community Projects - [ianriizky/rwid-git](https://github.com/ianriizky/rwid-git) +- [habibalftrh/rwid-git](https://github.com/habibalftrh/rwid-git) - **and more...** ## License diff --git a/functional/js/fibonacci.js b/functional/js/fibonacci.js index cfdb1fe..7493d7e 100644 --- a/functional/js/fibonacci.js +++ b/functional/js/fibonacci.js @@ -5,7 +5,20 @@ */ function generateFibonacciUsingLoop(sequence) { // write your code here - return []; + let fibonacci = [0, 1]; + + if (sequence == 1) { + fibonacci.pop(); + return fibonacci; + } else if (sequence == 2) { + return fibonacci; + } + + for (let index = 2; index < sequence; index++) { + fibonacci.push(fibonacci[index - 1] + fibonacci[index - 2]); + } + + return fibonacci; } /** @@ -15,7 +28,19 @@ function generateFibonacciUsingLoop(sequence) { */ function generateFibonacciUsingRecursive(sequence) { // write your code here - return []; + let array = []; + + if (sequence == 1) { + array.push(0); + } else if (sequence == 2) { + array.push(0, 1); + } else { + array = generateFibonacciUsingRecursive(sequence - 1); + + let len = array.length; + array.push(array[len - 1] + array[len - 2]); + } + return array; } document.getElementById("form").addEventListener("submit", function (event) { diff --git a/functional/js/palindrome.js b/functional/js/palindrome.js index 6d852fa..2305b2c 100644 --- a/functional/js/palindrome.js +++ b/functional/js/palindrome.js @@ -1,3 +1,4 @@ +import { parseString } from "../../js/helper.js"; /** * Determine whether the given value is a palindrome or not using reverse way. * @@ -5,7 +6,9 @@ */ function isPalindromeUsingReverse(value) { // write your code here - return true; + value = value.replace(/[^a-zA-Z]/g, "").toLowerCase(); + let reverseValuestr = value.split("").reverse().join(""); + return value == reverseValuestr; } /** @@ -15,6 +18,13 @@ function isPalindromeUsingReverse(value) { */ function isPalindromeUsingLoop(value) { // write your code here + value = value.replace(/[^a-zA-Z]/g, "").toLowerCase(); + let valueLen = value.length; + + for (let index = 1; index < valueLen / 2; index++) { + if (value[index] == value[valueLen - index - 1]) { + } + } return true; } @@ -26,7 +36,15 @@ function isPalindromeUsingLoop(value) { */ function isPalindromeUsingRecursive(value, index = 0) { // write your code here - return true; + + value = value.replace(/[^a-zA-Z]/g, "").toLowerCase(); + if (index >= Math.floor(value.length / 2)) { + return true; + } + if (value[index] !== value[value.length - 1 - index]) { + return false; + } + return isPalindromeUsingRecursive(value, index + 1); } document.getElementById("form").addEventListener("submit", function (event) { diff --git a/javascript-modules/generateMember.js b/javascript-modules/generateMember.js index e9eb0e1..2a1bff1 100644 --- a/javascript-modules/generateMember.js +++ b/javascript-modules/generateMember.js @@ -9,3 +9,4 @@ export { generateMember8 as m8 } from "./generateMember8.js"; export { generateMember9 as m9 } from "./generateMember9.js"; export { generateMember10 as m10 } from "./generateMember10.js"; export { generateMember11 as m11 } from "./generateMember11.js"; +export { generateMember12 as m12 } from "./generateMember12.js"; diff --git a/javascript-modules/generateMember12.js b/javascript-modules/generateMember12.js new file mode 100644 index 0000000..dfd5208 --- /dev/null +++ b/javascript-modules/generateMember12.js @@ -0,0 +1,15 @@ +import { Member } from "./Member.js"; + +function generateMember12() { + const member = new Member( + "Habib Al Fitrah", + 12, + "Makassar, Sulawesi Selatan", + "07", + "https://github.com/habibalftrh", + ); + + member.generateTrElement(); +} + +export { generateMember12 }; diff --git a/javascript-modules/main.js b/javascript-modules/main.js index cebbeff..2e4ca88 100644 --- a/javascript-modules/main.js +++ b/javascript-modules/main.js @@ -4,7 +4,7 @@ import { generateMentor } from "./generateMentor.js"; function main() { generateMentor(); - for (let index = 1; index <= 11; index++) { + for (let index = 1; index <= 12; index++) { generateMember[`m${index}`](); } } diff --git a/oop/js/Factorial.js b/oop/js/Factorial.js index ae6b23c..1e25277 100644 --- a/oop/js/Factorial.js +++ b/oop/js/Factorial.js @@ -12,6 +12,25 @@ export class Factorial { } // write your code here + calculateFactorialUsingLoop() { + if (this.n === 0 || this.n === 1) { + return 1; + } + + let factorial = 1; + for (let index = this.n; index > 0; index--) { + factorial *= index; + } + return factorial; + } + + calculateFactorialUsingRecursive(n = this.n) { + if (n < 2) { + return 1; + } + + return n * this.calculateFactorialUsingRecursive(n - 1); + } } document.getElementById("form").addEventListener("submit", function (event) { @@ -21,7 +40,17 @@ document.getElementById("form").addEventListener("submit", function (event) { const n = event.target["n"].value; const method = event.target["method"].value; - const result = new Factorial(n); + const resultObj = new Factorial(n); + + let result = 0; + + if (method == "loop") { + result = resultObj.calculateFactorialUsingLoop(); + } else if (method == "recursive") { + result = resultObj.calculateFactorialUsingRecursive(); + } else { + throw new Error("Method must be loop or recursive."); + } document.getElementById("result").textContent = result; } catch (error) { diff --git a/oop/js/Fibonacci.js b/oop/js/Fibonacci.js index 16e19f2..29d9af2 100644 --- a/oop/js/Fibonacci.js +++ b/oop/js/Fibonacci.js @@ -12,6 +12,35 @@ export class Fibonacci { } // write your code here + generateFibonacciUsingLoop() { + let fibonacci = [0, 1]; + if (this.sequence == 1) { + return fibonacci.pop(); + } else if (this.sequence == 2) { + return fibonacci; + } + + for (let index = 2; index < this.sequence; index++) { + fibonacci.push(fibonacci[index - 1] + fibonacci[index - 2]); + } + return fibonacci; + } + + generateFibonacciUsingRecursive(sequence = this.sequence) { + let array = []; + + if (sequence === 1) { + array.push(0); + } else if (sequence === 2) { + array.push(0, 1); + } else { + array = this.generateFibonacciUsingRecursive(sequence - 1); + + let len = array.length; + array.push(array[len - 1] + array[len - 2]); + } + return array; + } } document.getElementById("form").addEventListener("submit", function (event) { @@ -21,8 +50,15 @@ document.getElementById("form").addEventListener("submit", function (event) { const sequence = event.target["sequence"].value; const method = event.target["method"].value; - const result = new Fibonacci(sequence); - + let result = []; + const resultObj = new Fibonacci(sequence); + if (method === "loop") { + result = resultObj.generateFibonacciUsingLoop(sequence); + } else if (method === "recursive") { + result = resultObj.generateFibonacciUsingRecursive(sequence); + } else { + throw new Error("Method must be loop or recursive."); + } document.getElementById("result").textContent = JSON.stringify(result); } catch (error) { alert(error.message); diff --git a/oop/js/FizzBuzz.js b/oop/js/FizzBuzz.js index c34d9b8..a5e575b 100644 --- a/oop/js/FizzBuzz.js +++ b/oop/js/FizzBuzz.js @@ -11,19 +11,18 @@ export class FizzBuzz { this.sequence = parseNumber(sequence); } - generate() { + generateFizzBuzz(sequence = this.sequence) { let result = []; - for (let index = 1; index <= this.sequence; index++) { - if (index % 4 === 0 || index % 7 === 0) { + for (let index = 1; index <= sequence; index++) { + if (index % 4 == 0 || index % 7 == 0) { result.push("fizz buzz"); - } else if (index % 2 === 1) { + } else if (index % 2 == 1) { result.push("fizz"); - } else if (index % 2 === 0) { + } else if (index % 2 == 0) { result.push("buzz"); } } - return result; } } diff --git a/oop/js/Palindrome.js b/oop/js/Palindrome.js index b115542..6b616e5 100644 --- a/oop/js/Palindrome.js +++ b/oop/js/Palindrome.js @@ -1,3 +1,4 @@ +import { parseString } from "../../js/helper.js"; export class Palindrome { /** @type {string} */ value; @@ -10,6 +11,35 @@ export class Palindrome { } // write your code here + isPalindromeUsingReverse(value = this.value) { + value = value.replace(/[^a-zA-Z]/g, "").toLowerCase(); + let reverseValuestr = value.split("").reverse().join(""); + return value == reverseValuestr; + } + + isPalindromeUsingLoop(value = this.value) { + value = value.replace(/[^a-zA-Z]/g, "").toLowerCase(); + let valueLen = value.length; + + for (let index = 0; index < Math.floor(valueLen / 2); index++) { + if (value[index] !== value[valueLen - index - 1]) { + return false; + } + } + return true; + } + + isPalindromeUsingRecursive(value = this.value, index = 0) { + value = value.replace(/[^a-zA-Z]/g, "").toLowerCase(); + if (index >= Math.floor(value.length / 2)) { + return true; + } + if (value[index] !== value[value.length - 1 - index]) { + return false; + } + + return this.isPalindromeUsingRecursive(value, index + 1); + } } document.getElementById("form").addEventListener("submit", function (event) { @@ -19,7 +49,14 @@ document.getElementById("form").addEventListener("submit", function (event) { const word = event.target["word"].value; const method = event.target["method"].value; - const result = new Palindrome(word); + let result; + if (method === "reverse") { + result = new Palindrome(word).isPalindromeUsingReverse(word); + } else if (method === "loop") { + result = new Palindrome(word).isPalindromeUsingLoop(word); + } else if (method === "recursive") { + result = new Palindrome(word).isPalindromeUsingRecursive(word); + } document.getElementById("result").textContent = result; } catch (error) {