-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava.js
78 lines (71 loc) · 2.64 KB
/
java.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
let state = {
name: "",
todoList: [],
};
function getReply(command) {
if (command.startsWith("Hello my name is ")) {
const name = command.substring(17);
state.name = name;
return `Nice to meet you, ${name}.`;
} else if (command === "What is my name?") {
if (state.name) {
return `Your name is ${state.name}.`;
} else {
return "Sorry, I do not know your name.";
}
} else if (command.startsWith("Add ") && command.endsWith(" to my todo")) {
const activity = command
.substring(4, command.indexOf(" to my todo"))
.trim();
state.todoList.push(activity);
return `${activity} added to your todo.`;
} else if (command.startsWith("Remove ")) {
const removeItem = command
.substring(7, command.indexOf(" from my todo"))
.trim();
const index = state.todoList.indexOf(removeItem);
if (index !== -1) {
state.todoList.splice(index, 1);
return `Removed ${removeItem} from your todo.`;
} else {
return `${removeItem} is not in your todo.`;
}
} else if (command === "What is on my todo?") {
if (state.todoList.length === 0) {
return "There is nothing in your todo.";
} else {
const todoItems = state.todoList.join(" and ");
return `You have ${state.todoList.length} activity(ies) - ${todoItems}.`;
}
} else if (command === "What day is it today?") {
const today = new Date();
const options = { day: "numeric", month: "long", year: "numeric" };
const formattedDate = today.toLocaleDateString("en-US", options);
return `Today is ${formattedDate}.`;
} else if (command.startsWith("what is ")) {
const mathExpression = command.substring(8).trim();
const result = eval(mathExpression);
return `Result of ${mathExpression} is ${result}.`;
} else if (command.startsWith("Set a timer for ")) {
const timeInMinutes = parseInt(command.substring(16));
if (!isNaN(timeInMinutes)) {
setTimeout(() => {
console.log("Timer done.");
}, timeInMinutes * 60 * 1000);
return `Timer set for ${timeInMinutes} minutes.`;
} else {
return "Can't set timer. Please provide a valid time in minutes.";
}
} else {
return "Enter correct data.";
}
}
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 it today?"));
console.log(getReply("what is 3 + 3"));
console.log(getReply("Set a timer for 4 minutes"));