-
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/yuusuf #124
base: main
Are you sure you want to change the base?
Changes from all commits
b7d43e9
616bb28
ca5d1b7
91c6d70
b663c7a
5fd1f92
74d869b
473b5d6
966680c
6732bc2
c5db299
a0fc6b8
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,11 @@ | ||
<!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> | ||
<script src="voiceAssistant.js"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
let theirName = undefined; | ||
let todoList = []; | ||
|
||
|
||
|
||
function saveName(command){ | ||
let name = command.split(" ")[4]; | ||
theirName = name; | ||
return(`Nice to meet you${theirName}`); | ||
} | ||
|
||
|
||
function getName(){ | ||
if(theirName){ | ||
return (`You name is ${theirName}`); | ||
}else { | ||
return ("Who are You ahahhha"); | ||
} | ||
} | ||
|
||
|
||
function addTodoList(command){ | ||
let todo = command.slice(4, command.indexOf("to my todo")); | ||
todoList.push(todo); | ||
console.log( `${todo} added to you todo`); | ||
} | ||
|
||
|
||
function removeFromTodoList(command){ | ||
let todo = command.slice(7, command.indexOf("from my todo")); | ||
if (todoList.includes(todo)){ | ||
todoList.splice(todoList.indexOf(todo),1); | ||
return `I have removed ${todo} from you todo list`; | ||
|
||
}else { | ||
return `${todo} doesnt exist in your todo list`; | ||
} | ||
} | ||
|
||
|
||
function showOnTodoList(){ | ||
if(todoList.length === 0){ | ||
return("your todo list is empty"); | ||
}else{ | ||
console.log(`You have ${todoList.length} things to do ${todoList.join(",")}` ) | ||
} | ||
} | ||
|
||
function todaysDate(){ | ||
let day = new Date(); | ||
let today = day.getDate(); | ||
let monthNames = ["January", "February", "March", "April", "May", "June", | ||
"July", "August", "September", "October", "November", "December"]; | ||
let month = monthNames[day.getMonth()]; | ||
let year = day.getFullYear(); | ||
|
||
return `${today} ${month} ${year}` | ||
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. There's a neat (newish) method in Javascript called For example:
I'll let you figure out the rest 😉 |
||
} | ||
|
||
|
||
function calcMath(command){ | ||
let num1 = parseFloat(command.split(" ")[2]); | ||
let operator = command.split(" ")[3]; | ||
let num2 = parseFloat(command.split(" ")[4]); | ||
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. We're calling So consider calling |
||
|
||
|
||
switch (operator) { | ||
case "+": | ||
return num1 + num2; | ||
case "-": | ||
return num1 - num2; | ||
case "*": | ||
return num1 * num2; | ||
case "/": | ||
return num1 / num2; | ||
default: | ||
return "Im not able to solve that."; | ||
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 stuff having a fallback case if none of the others match. Consider adding some alternative or follow-up action for the user to help them understand how they can solve the error on their own. For example: |
||
} | ||
} | ||
|
||
|
||
|
||
function setTimer(command) { | ||
let minutes = parseInt(command.split(" ")[4]); | ||
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. How could you change this method if the user wanted to set a timer for say... 30 seconds? |
||
|
||
if (!minutes) { | ||
return "add valid number of minutes!"; | ||
} | ||
|
||
setTimeout(() => { | ||
console.log("timer done!"); | ||
}, minutes * 60000); | ||
|
||
return `Timer set for ${minutes} minutes.`; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
function getReply(command){ | ||
if(command.startsWith("Hi my name is")){ | ||
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 user types in this command using different casing? E.g. ALL UPPERCASE? Would be nice to have our code just handle that scenario! |
||
return saveName(command); | ||
} | ||
|
||
|
||
if(command == "What is my name?") { | ||
return getName(); | ||
} | ||
|
||
if(command.startsWith("add") && command.endsWith("to my todo")){ | ||
return addTodoList(command); | ||
}else if(command.startsWith("remove") && command.endsWith("from my todo")){ | ||
return removeFromTodoList(command); | ||
}else if(command === "what is on my todo?") { | ||
return showOnTodoList(); | ||
} | ||
|
||
if(command === "What day is it today?"){ | ||
return todaysDate(); | ||
} | ||
|
||
if (command.startsWith("what is")) { | ||
return `The answer is ${calcMath(command)}`; | ||
} | ||
|
||
if(command.startsWith("set a timer for")) { | ||
return setTimer(command); | ||
} | ||
|
||
return "Im not advanced enough to answer this." | ||
} |
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.
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: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 callcommand.indexOf("to my todo")
. That means it will assignthink of some things to add
to thetodo
variable, whereas it would ideally assignthink of some things to add to my todo
instead.We can fix this by using a slightly different method from
indexOf()
. Fortunately Javascript has alastIndexOf()
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!