-
Notifications
You must be signed in to change notification settings - Fork 16
finish challenge 2 recreate challenge 1 using node.js #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/pull-request/chapter-2
Are you sure you want to change the base?
finish challenge 2 recreate challenge 1 using node.js #20
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cause we will learn file management later on in the express.js basic course, I suggest that you locate the code as per the guidance below:
/src
for all of the JavaScript core code, such as factorial-functional, factorial-oop, fizzbuzz-functional, fizzbuzz-oop, etc. Also, put anyutils
,shared
, orhelpers
code in this folder./bin
for all the JavaScript code related to the terminal execution process./pages
for all the HTML page that has been created in Chapter 1, including some of the JavaScript code needed to run the HTML input like JavaScript DOM etc.
Thank you for the pull request.
bin/factorial/handleArgument.js
Outdated
|
||
for (let argument of cliArguments) { | ||
if (!validateArgument(argument)) { | ||
throw new Error(`invalid argument ${argument}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could make a better exception throwing by setting a specific Error
instance rather than just a basic Error
. See details here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#error_objects.
bin/shared/utils.js
Outdated
@@ -0,0 +1,3 @@ | |||
export const destructureArgument = argument => argument.split("="); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this file into /src
folder. I'll explain it later at the end of this review.
bin/factorial/index.js
Outdated
import { countFactorial } from "../../pages/functional/js/factorial.js"; | ||
|
||
import { Factorial } from "../../pages/oop/js/Factorial.js"; | ||
|
||
import { handleArgument } from "./handleArgument.js"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need extra line break for each part of the import code here.
bin/factorial/index.js
Outdated
let result = 0; | ||
|
||
if (!["oop", "functional"].includes(type)) { | ||
throw new Error(`invalid type ${type}. must be "oop" or "functional"`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #20 (comment).
bin/factorial/index.js
Outdated
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"`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put this validation code in the handleArgument
file so this file will be focused on running the action/logic part.
bin/factorial/index.js
Outdated
const factorial = new Factorial(n); | ||
result = factorial.count(method); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplify this by just trimming the code into one line.
pages/functional/js/factorial.js
Outdated
} | ||
} | ||
|
||
export { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #20 (comment).
@@ -0,0 +1,17 @@ | |||
import { countFactorial } from "../js/factorial.js"; | |||
|
|||
document.getElementById("form").addEventListener("submit", function (event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turn back this file into functional
folder. I'll explain it later at the end of this review.
Hello @ianriizky , I have already refactor 2nd challenge's code. this is the commit : refactor : move js to src and utility to utils Thanks, |
I found mistakes in previous commit, it is in input validation and I have fix it in here : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the review above, thank you.
let parsedInput = parseNumber(input); | ||
if (typeof parsedInput != "number") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add line break between this code.
@@ -0,0 +1,19 @@ | |||
export const validateFactorialMethod = method => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these function along with the validateApproach.js can be unified into one function. Then the required list of method (loop, recursive, functional, oop, etc.) can be passed as a paremeter of the function.
Hello, I have finished challenge 2 to recreate challenge 1 using nodejs
This is what I already do :
Thanks,
Dian P.