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

add homework #118

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

add homework #118

wants to merge 1 commit into from

Conversation

najmeh25
Copy link
Collaborator

@najmeh25 najmeh25 commented Dec 5, 2024

No description provided.

Copy link

@jmf-umbraco jmf-umbraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! I've left a few suggestions of things that can be improved slightly, but the bulk of the code works well.

One overall change you could consider is to pull out some of the code into helper methods. The getReply() function is doing quite a lot of work now - it's determining which command the user entered, extracting relevant info from that command, and then putting together a response. You could perhaps consider dividing up the code such that each different type of command is handled by a different function. This would help it all be a bit more readable at-a-glance, and would help control the size of the getReply() function if we ever enhanced the code to support many more commands!

function getReply(command) {

if (command.includes("Hello my name is")) {
let newName = command.split("Hello my name is ")[1];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, this approach works, which is the most important part of coding!

But it's a little unorthodox - the purpose of the .split() method on strings is to split them up into many different parts. What we actually are using it for is extracting one small part of a string, but there are some other methods that are just a little better suited for different reasons - they might perform faster, or be slightly easier for other developers to read and understand.

Consider using .subscring() or .slice() instead of .split()


function getReply(command) {

if (command.includes("Hello my name is")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the user doesn't use a starting capital? It would be nice if our program continued to work!

}
}

if (command.includes("Add") && command.includes("to my todo")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to consider using a slightly different condition instead of whether the command includes some text.

Consider what happens if the user enters the following command (and forget about casing for the moment):

Remove add things to my todo from my todo

Now this is a bit of a silly, contrived example, but the intent of the user is to remove add things to my todo from their todo list. On the other hand, our code is going to try and handle this as though the user wants to add something to their todo list, because it contains add and to my todo.

We can deal with this problem by being a bit more specific - perhaps we check that the command starts with add, not just that it contains that. Likewise we can check it ends with to my todo!

if (command.includes("Remove") && command.includes("from my todo")) {
let todoItem = command.split("Remove ")[1].split(" from my todo")[0];
let index = todos.indexOf(todoItem);
if (index > -1) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job handling this edge case!

];
let month = monthNames[today.getMonth()];
let year = today.getFullYear();
return `${day}. of ${month} ${year}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a neat (newish) method in Javascript called .toLocaleString() that will format a date based on some options you provide to it. It could be easier than doing the formatting this way.

For example:

let date = new Date();
let dateFormatOptions = {
    month: "long",
    year: "numeric"
};

return date.toLocaleString("en", dateFormatOptions);

I'll let you figure out the rest 😉

} else if (operator === "*") {
return num1 * num2;
} else if (operator === "/") {
return num2 !== 0 ? num1 / num2 : "Cannot divide by zero";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice edge case handling!

} else if (operator === "/") {
return num2 !== 0 ? num1 / num2 : "Cannot divide by zero";
} else {
return "I can only perform +, -, *, and / operations";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great error message. We frequently are guilty of writing error messages that say things like "Invalid input", or some variation on "I can't do that". This can be frustrating to the user - because from their perspective they have no idea what's actually wrong, or what else they can try.

Your error message instead helps the user to understand how the program does work and gives clues about what the user can do about the error. It's much more helpful!

.replace("Set a timer for", "")
.replace("minutes", "")
.trim();
let minutes = Number(timePart);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again, this works, but there might be some easier ways of extracting the number from the string. Can you think of any?

@najmeh25
Copy link
Collaborator Author

najmeh25 commented Dec 11, 2024 via email

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