Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

GuzideGuzelbey
Copy link
Collaborator

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 :)

Copy link

@Khandohii Khandohii left a 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);

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.

Copy link
Collaborator Author

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 :)

Comment on lines +104 to +123
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.";
}

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.

Copy link
Collaborator Author

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?

@GuzideGuzelbey
Copy link
Collaborator Author

Overall, thanks for the review and the constructive feedbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants