generated from HackYourFuture-CPH/hyf-homework-template
-
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/guzide #122
Open
GuzideGuzelbey
wants to merge
2
commits into
main
Choose a base branch
from
javascript-javascript1-week4/guzide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>Voice Assistant</title> | ||
</head> | ||
<body> | ||
<script src="voiceAssistant.js"></script> | ||
</body> | ||
</html> |
157 changes: 157 additions & 0 deletions
157
javascript/javascript1/week4/week4-guzide/voiceAssistant.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
const toDo = []; | ||
let userName = ""; | ||
|
||
function getReply(command) { | ||
if (command.startsWith("Hello my name is")) { | ||
return setUserName(command); | ||
} | ||
|
||
if (command.startsWith("What is my name?")) { | ||
return getUserName(); | ||
} | ||
|
||
if (command.startsWith("Add")) { | ||
return addToDo(command); | ||
} | ||
|
||
if (command.startsWith("Remove")) { | ||
return removeToDo(command); | ||
} | ||
|
||
if (command === "What is on my todo?") { | ||
return showToDos(); | ||
} | ||
|
||
if (command === "What day is it today?") { | ||
return getAndFormatDate(); | ||
} | ||
|
||
if (command.startsWith("what is")) { | ||
return calculate(command); | ||
} | ||
|
||
if (command.startsWith("Set a timer for")) { | ||
return setTimer(command); | ||
} | ||
|
||
if (command === "What is the purpose of life?") { | ||
return "To find your unique path!"; | ||
} | ||
} | ||
|
||
function setUserName(input) { | ||
userName = input.split(" ").slice(4).join(" "); | ||
return `Nice to meet you ${userName.toLowerCase()}`; | ||
} | ||
|
||
function getUserName() { | ||
if (userName) { | ||
return `Your name is ${userName}`; | ||
} else { | ||
return "Please, first save your name!"; | ||
} | ||
} | ||
|
||
function addToDo(input) { | ||
const inputArray = input.split(" "); | ||
const taskToDo = inputArray.slice(1, inputArray.length - 3).join(" "); | ||
toDo.push(taskToDo); | ||
return `${taskToDo.toLowerCase()} added to your todo.`; | ||
} | ||
|
||
function removeToDo(input) { | ||
const inputArray = input.split(" "); | ||
const taskToRemove = inputArray.slice(1, inputArray.length - 3).join(" "); | ||
const taskToRemoveIndex = toDo.indexOf(taskToRemove); | ||
if (toDo.includes(taskToRemove)) { | ||
toDo.splice(taskToRemoveIndex, 1); | ||
return `Removed ${taskToRemove} from your todo.`; | ||
} else { | ||
return `${taskToRemove} is not in your todo.`; | ||
} | ||
} | ||
|
||
function showToDos() { | ||
if (toDo.length > 0) { | ||
return `You have ${toDo.length} todos - ${toDo.join(", ")}.`; | ||
} else { | ||
return "Your todo list is empty."; | ||
} | ||
} | ||
|
||
function getAndFormatDate() { | ||
const today = new Date(); | ||
const day = today.getDate(); | ||
const months = [ | ||
"January", | ||
"February", | ||
"March", | ||
"April", | ||
"May", | ||
"June", | ||
"July", | ||
"August", | ||
"September", | ||
"October", | ||
"November", | ||
"December", | ||
]; | ||
const month = months[today.getMonth()]; | ||
const year = today.getFullYear(); | ||
return `${day}. of ${month} ${year}`; | ||
} | ||
|
||
function calculate(input) { | ||
const numbers = input.match(/\d+/g).map(Number); | ||
const operators = input.match(/[\+\-\*\/]/)?.[0]; | ||
if (numbers?.length === 2 && operators) { | ||
const [a, b] = numbers; | ||
switch (operators) { | ||
case "+": | ||
return a + b; | ||
case "-": | ||
return a - b; | ||
case "*": | ||
return a * b; | ||
case "/": | ||
return b !== 0 ? a / b : "Cannot divide by zero"; | ||
default: | ||
return "Sorry, this operator is not supported."; | ||
} | ||
} | ||
return "Error! Invalid calculation command."; | ||
} | ||
|
||
function setTimer(input) { | ||
const time = input.match(/Set a timer for (\d+) (minute|minutes)/i); | ||
|
||
if (time) { | ||
const minutes = parseInt(time[1], 10); | ||
console.log( | ||
`Timer set for ${minutes} ${ | ||
minutes === 1 ? "minute" : "minutes" | ||
} minutes.` | ||
); | ||
const milliseconds = minutes * 60 * 1000; | ||
setTimeout(() => { | ||
console.log("Timer done."); | ||
}, milliseconds); | ||
return "Counting"; | ||
} else { | ||
return "Invalid command. Please use the format: 'Set a timer for n minutes' or 'Set a timer for 1 minute'."; | ||
} | ||
} | ||
|
||
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("Remove fishing from my todo")); | ||
console.log(getReply("Add singing in the shower to my todo")); | ||
console.log(getReply("What is on my todo?")); | ||
console.log(getReply("What is the purpose of life?")); | ||
console.log(getReply("Set a timer for 1 minutes")); //You should wait for that x min to see the full response | ||
console.log(getReply("Set a timer for time minutes")); | ||
console.log(getReply("what is 4 * 12")); | ||
console.log(getReply("what is 89 / 12")); | ||
console.log(getReply("what is 4 = 28")); | ||
console.log(getReply("What day is it today?")); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 solution. For solving mathematical calculations you can use
eval()
function as well.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.
Hi Khandohii, thank you. But while I was searching for the methods to be used I came up with this info "eval() executes any JavaScript code in the input string. This makes it vulnerable to injection attacks." That is why I did not use it. What do you think about this info?