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/ruslana #121

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
13 changes: 13 additions & 0 deletions javascript/javascript1/week4/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Homework 4</h1>
<p>Java Script1, week 4</p>
<script src="./script.js"></script>
</body>
</html>
98 changes: 98 additions & 0 deletions javascript/javascript1/week4/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
let userName = null;
let todoList = [];

function getReply(command) {
const lowerCaseCommand = command.toLowerCase();

Choose a reason for hiding this comment

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

Good thinking! It's always nice to make an effort to handle slightly incorrect user input, such as casing.


if (lowerCaseCommand.startsWith('hello my name is ')) {
const name = command.slice(17);
if (userName === name) {
return `You already introduced yourself as ${name}.`;
} else {
userName = name;
return `Nice to meet you ${name}`;
}
}

if (lowerCaseCommand === 'what is my name?') {
return userName
? `Your name is ${userName}`
: "I don't know your name yet. Please tell me your name first.";
}

if (
lowerCaseCommand.startsWith('add ') &&
lowerCaseCommand.includes(' to my todo')
) {
const todo = command.slice(4, command.indexOf(' to my todo'));

Choose a reason for hiding this comment

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

It's not something super important to handle, but it's always good to think about edge cases and how our code might behave.

For example, what if someone wanted to add think of some things to add to my todo to their todo list? Their command would be:

add think of some things to add to my todo to my todo

Which is a little silly for sure! But it would be nice if our code handled it.

What the code will currently do is see the first instance of to my todo when we call command.indexOf("to my todo"). That means it will assign think of some things to add to the todo variable, whereas it would ideally assign think of some things to add to my todo instead.

We can fix this by using a slightly different method from indexOf(). Fortunately Javascript has a lastIndexOf() method for exactly this reason. Instead of returning the index of the first matching string, it will return the index of the last one!

Again - not a super important fix for now. The lesson is mostly just to keep thinking about our code and how it might break, so that we can make deliberate choices about whether we fix it!

todoList.push(todo);
return `${todo} added to your todo.`;
}

if (
lowerCaseCommand.startsWith('remove ') &&
lowerCaseCommand.includes(' from my todo')
) {
const todo = command.slice(7, command.indexOf(' from my todo'));
const index = todoList.indexOf(todo);
if (index > -1) {

Choose a reason for hiding this comment

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

Great catch. Calling .splice() with -1 as an argument would have very unexpected results!

todoList.splice(index, 1);
return `Removed ${todo} from your todo.`;
} else {
return `${todo} is not in your todo list.`;
}
}

if (lowerCaseCommand === 'what is on my todo?') {
if (todoList.length === 0) {
return 'Your todo list is empty.';
} else {
return `You have ${todoList.length} todos: ${todoList.join(', ')}.`;
}
}

if (lowerCaseCommand === 'what day is it today?') {
const today = new Date();
const day = today.getDate();
const month = today.toLocaleString('default', { month: 'long' });

Choose a reason for hiding this comment

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

Great use of some newer Javascript functionality - toLocaleString(). Consider using it for the whole date format though, not just the month!

const year = today.getFullYear();
return `${day}. of ${month} ${year}`;
}

if (lowerCaseCommand.startsWith('what is ')) {
const mathExpression = command.slice(8);
try {
const result = eval(mathExpression);

Choose a reason for hiding this comment

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

While eval() is very useful for cases like this where we want to evaluate an expression, we need to be very careful about using it as developers.

Many sites will disable using it entirely these days, but consider what happens if the user types

what is alert(`hello ${userName}`)

That's right - this user input will cause the browser (if running in the browser) to pop up an alert dialog, and it's even got access to the value of any variables! Consider that this could be used to send information about the user to a malicious site, and you'll start to realise why eval() is generally considered best left alone!

That said, for this assignment, it's a great way to get started.

An alternative could be to parse some numbers and an operator (+, -, *, /) form the command string, and then perform a different operation based on the operator.

return `${mathExpression} is ${result}`;
} catch (error) {
return "Sorry, I couldn't calculate that.";
}
}

if (
lowerCaseCommand.startsWith('set a timer for ') &&
lowerCaseCommand.includes(' minutes')
) {
const minutes = parseInt(command.slice(16, command.indexOf(' minutes')));
if (isNaN(minutes)) {

Choose a reason for hiding this comment

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

Good error handling!

return "I couldn't understand the time duration.";
}
setTimeout(() => {
console.log('Timer done!');
}, minutes * 60 * 1000);
return `Timer set for ${minutes} minutes.`;
}

return "Sorry, I didn't understand that command.";
}

console.log(getReply('Hello my name is Benjamin'));
console.log(getReply('What is my name?'));
console.log(getReply('Add fishing to my todo'));
console.log(getReply('Add singing in the shower to my todo'));
console.log(getReply('What is on my todo?'));
console.log(getReply('Remove fishing from my todo'));
console.log(getReply('What is on my todo?'));
console.log(getReply('What day is it today?'));
console.log(getReply('What is 3 + 3'));
console.log(getReply('Set a timer for 1 minutes'));
12 changes: 12 additions & 0 deletions javascript/javascript1/week4/tasks-from-the-class/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tasks from the class</title>
</head>
<body>

<script src="./script.js"></script>
</body>
</html>
Loading