From 52cbd7febac1160c52a0f6cf193a59d72a9ea262 Mon Sep 17 00:00:00 2001 From: GuzideGuzelbey Date: Fri, 6 Dec 2024 16:44:56 +0100 Subject: [PATCH 1/2] moved the week4 practice to a new branch from main which was mistakenly branched out from week3 --- .../javascript1/week4/week4-guzide/index.html | 11 ++ .../week4/week4-guzide/voiceAssistant.js | 153 ++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 javascript/javascript1/week4/week4-guzide/index.html create mode 100644 javascript/javascript1/week4/week4-guzide/voiceAssistant.js diff --git a/javascript/javascript1/week4/week4-guzide/index.html b/javascript/javascript1/week4/week4-guzide/index.html new file mode 100644 index 0000000..a2f0b78 --- /dev/null +++ b/javascript/javascript1/week4/week4-guzide/index.html @@ -0,0 +1,11 @@ + + + + + + Voice Assistant + + + + + diff --git a/javascript/javascript1/week4/week4-guzide/voiceAssistant.js b/javascript/javascript1/week4/week4-guzide/voiceAssistant.js new file mode 100644 index 0000000..fdcf28f --- /dev/null +++ b/javascript/javascript1/week4/week4-guzide/voiceAssistant.js @@ -0,0 +1,153 @@ +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+) minutes/i); + + if (time) { + const minutes = parseInt(time[1], 10); + console.log(`Timer set for ${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'."; + } +} + +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 2 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?")); From 98db665828fa943cf2ad89ac53b84af3438c8f00 Mon Sep 17 00:00:00 2001 From: GuzideGuzelbey Date: Thu, 19 Dec 2024 12:13:18 +0100 Subject: [PATCH 2/2] validation for 1 minute added --- .../javascript1/week4/week4-guzide/voiceAssistant.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/javascript/javascript1/week4/week4-guzide/voiceAssistant.js b/javascript/javascript1/week4/week4-guzide/voiceAssistant.js index fdcf28f..b0ce9f3 100644 --- a/javascript/javascript1/week4/week4-guzide/voiceAssistant.js +++ b/javascript/javascript1/week4/week4-guzide/voiceAssistant.js @@ -123,18 +123,22 @@ function calculate(input) { } function setTimer(input) { - const time = input.match(/Set a timer for (\d+) minutes/i); + 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.`); + 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'."; + return "Invalid command. Please use the format: 'Set a timer for n minutes' or 'Set a timer for 1 minute'."; } } @@ -145,7 +149,7 @@ 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 2 minutes")); //You should wait for that x min to see the full response +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"));