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/guzide #122

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions javascript/javascript1/week4/week4-guzide/index.html
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 javascript/javascript1/week4/week4-guzide/voiceAssistant.js
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.";
}
Comment on lines +104 to +123

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.

Copy link
Collaborator Author

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?


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?"));