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

VoiceAssistent execise #119

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
111 changes: 111 additions & 0 deletions javascript/javascript1/week4/VoiceAssistent.js
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")) {

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!

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

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The 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" });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of .toLocaleString(), but we might be able to save ourselves some further effort by using it for the day and year as well!

Also, for real apps we should consider whether en-US is the correct choice of culture. If the rest of your app is in English, then perhaps just en without a country code is preferable. If your app is localised such that users see their native language anywhere, then we might want to use default so that the date doesn't appear in English!

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.";

Choose a reason for hiding this comment

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