-
Notifications
You must be signed in to change notification settings - Fork 0
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
Javascript javascript1 week4/guzide #122
base: main
Are you sure you want to change the base?
Conversation
…ly branched out from week3
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.
Good job @GuzideGuzelbey 👍
Left some comments 😊
} | ||
|
||
function setTimer(input) { | ||
const time = input.match(/Set a timer for (\d+) minutes/i); |
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.
What if to set timer for "1 minute". I would add a validation for "minute" as well.
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.
Validation is added, thank you :)
function calculate(input) { | ||
const numbers = input.match(/\d+/g).map(Number); | ||
const operators = input.match(/[\+\-\*\/]/)?.[0]; | ||
if (numbers?.length === 2 && operators) { | ||
const [a, b] = numbers; | ||
switch (operators) { | ||
case "+": | ||
return a + b; | ||
case "-": | ||
return a - b; | ||
case "*": | ||
return a * b; | ||
case "/": | ||
return b !== 0 ? a / b : "Cannot divide by zero"; | ||
default: | ||
return "Sorry, this operator is not supported."; | ||
} | ||
} | ||
return "Error! Invalid calculation command."; | ||
} |
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.
Good solution. For solving mathematical calculations you can use eval()
function as well.
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.
Hi Khandohii, thank you. But while I was searching for the methods to be used I came up with this info "eval() executes any JavaScript code in the input string. This makes it vulnerable to injection attacks." That is why I did not use it. What do you think about this info?
Overall, thanks for the review and the constructive feedbacks. |
Thanks for the challenging exercise, huh.
Moved the week4 practice to a new branch from main which was mistakenly branched out from week3.
So I am creating the request again :)