diff --git a/4-typing-game/solution/index.js b/4-typing-game/solution/index.js
index 646c38a34e..91f5a0428f 100644
--- a/4-typing-game/solution/index.js
+++ b/4-typing-game/solution/index.js
@@ -31,7 +31,7 @@ document.getElementById('start').addEventListener('click', function () {
// UI updates
// Create an array of span elements so we can set a class
- const spanWords = words.map(function(word) { return `${word} `});
+ const spanWords = words.map(function (word) { return `${word} ` });
// Convert into string and set as innerHTML on quote display
quoteElement.innerHTML = spanWords.join('');
// Highlight the first word
@@ -83,3 +83,12 @@ typedValueElement.addEventListener('input', (e) => {
typedValueElement.className = 'error';
}
});
+
+// Add this at the end of the file
+const messages = {
+ success: "CONGRATULATIONS! You finished in {seconds} seconds.",
+ error: "Oops! There's a mistake.",
+ start: "Start typing to begin the game."
+};
+
+export default messages;
diff --git a/7-bank-project/solution/app.js b/7-bank-project/solution/app.js
index 5ac4826e04..21f1096842 100644
--- a/7-bank-project/solution/app.js
+++ b/7-bank-project/solution/app.js
@@ -4,6 +4,7 @@
const serverUrl = 'http://localhost:5000/api';
const storageKey = 'savedAccount';
+const accountsKey = 'accounts'; // New key for all accounts
// ---------------------------------------------------------------------------
// Router
@@ -32,7 +33,7 @@ function updateRoute() {
const app = document.getElementById('app');
app.innerHTML = '';
app.appendChild(view);
-
+
if (typeof route.init === 'function') {
route.init();
}
@@ -41,32 +42,75 @@ function updateRoute() {
}
// ---------------------------------------------------------------------------
-// API interactions
+// API interactions (replaced with localStorage logic)
// ---------------------------------------------------------------------------
-async function sendRequest(api, method, body) {
- try {
- const response = await fetch(serverUrl + api, {
- method: method || 'GET',
- headers: body ? { 'Content-Type': 'application/json' } : undefined,
- body
- });
- return await response.json();
- } catch (error) {
- return { error: error.message || 'Unknown error' };
- }
+function getAccounts() {
+ return JSON.parse(localStorage.getItem(accountsKey) || '[]');
+}
+
+function saveAccounts(accounts) {
+ localStorage.setItem(accountsKey, JSON.stringify(accounts));
+}
+
+function findAccount(user) {
+ const accounts = getAccounts();
+ return accounts.find(acc => acc.user === user) || null;
}
async function getAccount(user) {
- return sendRequest('/accounts/' + encodeURIComponent(user));
+ // Simulate async
+ return new Promise(resolve => {
+ setTimeout(() => {
+ const acc = findAccount(user);
+ if (!acc) resolve({ error: 'Account not found' });
+ else resolve(acc);
+ }, 100);
+ });
}
-async function createAccount(account) {
- return sendRequest('/accounts', 'POST', account);
+async function createAccount(accountJson) {
+ return new Promise(resolve => {
+ setTimeout(() => {
+ let data;
+ try {
+ data = JSON.parse(accountJson);
+ } catch (e) {
+ return resolve({ error: 'Malformed account data' });
+ }
+ if (!data.user) return resolve({ error: 'Username required' });
+ if (findAccount(data.user)) return resolve({ error: 'User already exists' });
+ // Set up initial account structure
+ const newAcc = {
+ user: data.user,
+ description: data.description || '',
+ balance: 0,
+ currency: data.currency || 'USD',
+ transactions: []
+ };
+ const accounts = getAccounts();
+ accounts.push(newAcc);
+ saveAccounts(accounts);
+ resolve(newAcc);
+ }, 100);
+ });
}
-async function createTransaction(user, transaction) {
- return sendRequest('/accounts/' + user + '/transactions', 'POST', transaction);
+async function createTransaction(user, transactionJson) {
+ return new Promise(resolve => {
+ setTimeout(() => {
+ const accounts = getAccounts();
+ const idx = accounts.findIndex(acc => acc.user === user);
+ if (idx === -1) return resolve({ error: 'Account not found' });
+ const tx = JSON.parse(transactionJson);
+ tx.amount = parseFloat(tx.amount);
+ tx.date = tx.date || new Date().toISOString().slice(0, 10);
+ accounts[idx].balance += tx.amount;
+ accounts[idx].transactions.push(tx);
+ saveAccounts(accounts);
+ resolve(tx);
+ }, 100);
+ });
}
// ---------------------------------------------------------------------------