-
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
VoiceAssistent execise #119
base: main
Are you sure you want to change the base?
Changes from all commits
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,111 @@ | ||
let userName = ""; | ||
const todos = []; | ||
|
||
function getReply(command) { | ||
// Name Part | ||
if (command.startsWith("Hello my name is")) { | ||
const name = command.replace("Hello my name is ", ""); | ||
if (userName === name) { | ||
return `We already met, ${name}.`; | ||
} else { | ||
userName = name; | ||
return `Nice to meet you ${name}`; | ||
} | ||
} | ||
|
||
|
||
if (command === "What is my name?") { | ||
if (userName === "") { | ||
return "I don't know your name yet."; | ||
} else { | ||
return `Your name is ${userName}`; | ||
} | ||
} | ||
|
||
// Todos Part | ||
if (command.startsWith("Add") && command.endsWith("to my todo")) { | ||
const task = command.replace("Add ", "").replace(" 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. What happens if the thing that the user wants to add to their todo list includes the text "Add "? |
||
todos.push(task); | ||
return `${task} added to your todo`; | ||
} | ||
|
||
if (command.startsWith("Remove") && command.endsWith("from my todo")) { | ||
const task = command.replace("Remove ", "").replace(" from my todo", ""); | ||
const index = todos.indexOf(task); | ||
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. Good edge case handling! |
||
todos.splice(index, 1); | ||
return `Removed ${task} from your todo`; | ||
} else { | ||
return `${task} is not in your todo`; | ||
} | ||
} | ||
|
||
if (command === "What is on my todo?") { | ||
if (todos.length === 0) { | ||
return "You have no todos."; | ||
} else { | ||
return `You have ${todos.length} todos: ${todos.join(", ")}`; | ||
} | ||
} | ||
|
||
// Day Part | ||
if (command === "What day is today?") { | ||
const today = new Date(); | ||
const day = today.getDate(); | ||
const month = today.toLocaleString("en-US", { 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. Good use of Also, for real apps we should consider whether |
||
const year = today.getFullYear(); | ||
return `${day}. of ${month} ${year}`; | ||
} | ||
|
||
|
||
if (command.startsWith("What is")) { | ||
let [num1, operator, num2] = command.replace("What is ", "").split(" "); | ||
num1 = parseFloat(num1); | ||
num2 = parseFloat(num2); | ||
|
||
if (isNaN(num1) || isNaN(num2)) { | ||
return "I couldn't understand the numbers."; | ||
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. Consider using an error message that gives the user more clues about how they should proceed. For example, you could help the user understand how they should be formatted, or let them know that perhaps they're missing some whitespace between the numbers and operators. |
||
} | ||
|
||
switch (operator) { | ||
case "+": return `${num1 + num2}`; | ||
case "-": return `${num1 - num2}`; | ||
case "*": return `${num1 * num2}`; | ||
case "/": return `${num1 / num2}`; | ||
} | ||
} | ||
|
||
|
||
|
||
if (command.startsWith("Set a timer for")) { | ||
let time = parseInt(command.replace("Set a timer for ", "").replace(" minutes", "")); | ||
|
||
if (!isNaN(time)) { | ||
setTimeout(() => { | ||
console.log("Timer done"); | ||
}, time * 60000); | ||
|
||
return `Timer set for ${time} minutes`; | ||
} else { | ||
return "I couldn't understand the time."; | ||
} | ||
} | ||
return "I don't understand that command."; | ||
} | ||
|
||
|
||
|
||
// console.logs | ||
console.log(getReply("Hello my name is Benjamin")); | ||
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("Remove fishing from my todo")); | ||
console.log(getReply("What is on my todo?")); | ||
console.log(getReply("What day is today?")); | ||
console.log(getReply("What is 2 + 3?")); | ||
console.log(getReply("What is 5 * 1?")); | ||
console.log(getReply("What is 10 / 2?")); | ||
console.log(getReply("What is 10 - 5?")); | ||
console.log(getReply("Set a timer for 1 minutes")); |
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 happens if the user types the command in all lower case? Would be nice if our code just handled such a situation!