-
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/ruslana #121
base: main
Are you sure you want to change the base?
Changes from all commits
cd1d733
2359647
e9f6957
1cb8a4f
841f30c
192e151
35f415a
a53e48a
0e17b01
dcae9ed
360422d
7944c43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
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(); | ||
|
||
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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 We can fix this by using a slightly different method from 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch. Calling |
||
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' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of some newer Javascript functionality - |
||
const year = today.getFullYear(); | ||
return `${day}. of ${month} ${year}`; | ||
} | ||
|
||
if (lowerCaseCommand.startsWith('what is ')) { | ||
const mathExpression = command.slice(8); | ||
try { | ||
const result = eval(mathExpression); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While Many sites will disable using it entirely these days, but consider what happens if the user types
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 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')); |
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> |
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 thinking! It's always nice to make an effort to handle slightly incorrect user input, such as casing.