-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (145 loc) · 5.14 KB
/
script.js
File metadata and controls
174 lines (145 loc) · 5.14 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const startButton = document.getElementById('start-button');
const resultDiv = document.getElementById('result');
const historyList = document.getElementById('history-list');
const clearHistoryButton = document.querySelector('.clear-history');
const usernameDisplay = document.getElementById('username-display');
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.interimResults = false;
recognition.lang = 'en-US';
function displayUsername() {
const username = sessionStorage.getItem('sessionUser');
if (username) {
usernameDisplay.textContent = `Welcome, ${username}!`;
} else {
usernameDisplay.textContent = '';
}
}
window.onload = () => {
displayUsername();
};
startButton.addEventListener('click', () => {
recognition.start();
updateResult('', 'Listening...');
startButton.disabled = true;
updateButtonState(true);
});
recognition.addEventListener('result', (event) => {
const transcript = event.results[0][0].transcript;
updateResult(`You said: ${transcript}`, '');
calculateResult(transcript);
});
recognition.addEventListener('end', () => {
const currentResult = resultDiv.querySelector('.result-text')?.textContent || '';
updateResult(currentResult, '(Stopped listening)');
updateButtonState(false);
startButton.disabled = false;
});
recognition.addEventListener('start', () => {
updateButtonState(true);
});
function updateButtonState(isListening) {
startButton.innerHTML = isListening
? `<span class="listening-indicator"></span>Listening...`
: `<i class="fas fa-microphone"></i>Start Listening`;
}
function updateResult(mainText, statusText) {
resultDiv.innerHTML = `
${mainText ? `<div class="result-text ${mainText.startsWith('Error:') ? 'error' : ''}">${mainText}</div>` : ''}
${statusText ? `<div class="result-status">${statusText}</div>` : ''}
`;
}
function calculateResult(input) {
try {
const lowerInput = input.toLowerCase().replace(/what is|calculate|find|tell me/g, '').trim();
if (lowerInput.includes('percent of') || lowerInput.includes('% of')) {
calculatePercentOf(lowerInput);
return;
}
if (lowerInput.includes('square root of')) {
calculateSquareRoot(lowerInput);
return;
}
if (lowerInput.includes('square of')) {
calculateSquare(lowerInput);
return;
}
if (lowerInput.includes('celsius to fahrenheit')) {
convertCelsiusToFahrenheit(lowerInput);
return;
}
if (lowerInput.includes('fahrenheit to celsius')) {
convertFahrenheitToCelsius(lowerInput);
return;
}
evaluateExpression(lowerInput, input);
} catch (error) {
updateResult(`Error: ${error.message}`, '');
}
}
function calculatePercentOf(input) {
let parts;
if (input.includes('percent of')) {
parts = input.split('percent of');
} else if (input.includes('% of')) {
parts = input.split('% of');
} else {
return;
}
const value = extractNumber(parts[0]);
const total = extractNumber(parts[1]);
const result = (value / 100) * total;
displayResult(`${value} percent of ${total} = ${result}`, result);
}
function calculateSquareRoot(input) {
const value = extractNumber(input);
const result = Math.sqrt(value);
displayResult(`Square root of ${value} = ${result}`, result);
}
function calculateSquare(input) {
const value = extractNumber(input);
const result = Math.pow(value, 2);
displayResult(`Square of ${value} = ${result}`, result);
}
function convertCelsiusToFahrenheit(input) {
const value = extractNumber(input);
const result = (value * 9 / 5) + 32;
displayResult(`${value} Celsius to Fahrenheit = ${result}`, result);
}
function convertFahrenheitToCelsius(input) {
const value = extractNumber(input);
const result = (value - 32) * 5 / 9;
displayResult(`${value} Fahrenheit to Celsius = ${result}`, result);
}
function evaluateExpression(lowerInput, originalInput) {
const formattedInput = lowerInput
.replace(/plus/g, '+')
.replace(/minus/g, '-')
.replace(/times/g, '*')
.replace(/multiplied by/g, '*')
.replace(/divided by/g, '/')
.replace(/x/g, '*');
const result = eval(formattedInput);
displayResult(`${originalInput} = ${result}`, result);
}
function extractNumber(input) {
const matches = input.match(/-?\d+(\.\d+)?/);
return matches ? parseFloat(matches[0]) : 0;
}
function displayResult(resultText, result) {
updateResult(resultText, '');
resultDiv.classList.remove('error');
addToHistory(resultText);
}
function addToHistory(calculation) {
const listItem = document.createElement('li');
listItem.textContent = calculation;
historyList.prepend(listItem);
if (historyList.children.length > 10) {
historyList.removeChild(historyList.lastChild);
}
}
clearHistoryButton.addEventListener('click', () => {
while (historyList.firstChild) {
historyList.removeChild(historyList.firstChild);
}
});