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/yuusuf #124

Open
wants to merge 12 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/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>Document</title>
</head>
<body>
<script src="voiceAssistant.js"></script>
</body>
</html>
135 changes: 135 additions & 0 deletions javascript/javascript1/week4/voiceAssistant.js
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"));

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:

add think of some things to add to my todo to my todo

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 call command.indexOf("to my todo"). That means it will assign think of some things to add to the todo variable, whereas it would ideally assign think of some things to add to my todo instead.

We can fix this by using a slightly different method from indexOf(). Fortunately Javascript has a lastIndexOf() 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!

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}`

Choose a reason for hiding this comment

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

There's a neat (newish) method in Javascript called .toLocaleString() that will format a date based on some options you provide to it. It could be easier than doing the formatting this way.

For example:

let date = new Date();
let dateFormatOptions = {
    month: "long",
    year: "numeric"
};

return date.toLocaleString("en", dateFormatOptions);

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]);

Choose a reason for hiding this comment

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

We're calling command.split(" ") three times here on these three lines. While that's not a big problem for short strings, in general we want our programs to do the least work they can get away with. That helps us write fast performant code that doesn't frustrate users, and it helps us save the planet a little at a time by using less computing power too!

So consider calling command.split(" ") just once and assigning the result to a variable. You can then use that variable on each of these lines.



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

Choose a reason for hiding this comment

The 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: I'm not able to solve that. Try again using a +, -, *, or / operator in your equation

}
}



function setTimer(command) {
let minutes = parseInt(command.split(" ")[4]);

Choose a reason for hiding this comment

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

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