Skip to content

Commit 1230bc7

Browse files
authored
Merge pull request #1437 from yashshinde8585/fix-bank-api
Fix: Removed broken API request from 7.bank solution (Fixes #1384)
2 parents 3f07fd3 + 3fcb13e commit 1230bc7

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

4-typing-game/solution/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ document.getElementById('start').addEventListener('click', function () {
3131

3232
// UI updates
3333
// Create an array of span elements so we can set a class
34-
const spanWords = words.map(function(word) { return `<span>${word} </span>`});
34+
const spanWords = words.map(function (word) { return `<span>${word} </span>` });
3535
// Convert into string and set as innerHTML on quote display
3636
quoteElement.innerHTML = spanWords.join('');
3737
// Highlight the first word
@@ -83,3 +83,12 @@ typedValueElement.addEventListener('input', (e) => {
8383
typedValueElement.className = 'error';
8484
}
8585
});
86+
87+
// Add this at the end of the file
88+
const messages = {
89+
success: "CONGRATULATIONS! You finished in {seconds} seconds.",
90+
error: "Oops! There's a mistake.",
91+
start: "Start typing to begin the game."
92+
};
93+
94+
export default messages;

7-bank-project/solution/app.js

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const serverUrl = 'http://localhost:5000/api';
66
const storageKey = 'savedAccount';
7+
const accountsKey = 'accounts'; // New key for all accounts
78

89
// ---------------------------------------------------------------------------
910
// Router
@@ -32,7 +33,7 @@ function updateRoute() {
3233
const app = document.getElementById('app');
3334
app.innerHTML = '';
3435
app.appendChild(view);
35-
36+
3637
if (typeof route.init === 'function') {
3738
route.init();
3839
}
@@ -41,32 +42,75 @@ function updateRoute() {
4142
}
4243

4344
// ---------------------------------------------------------------------------
44-
// API interactions
45+
// API interactions (replaced with localStorage logic)
4546
// ---------------------------------------------------------------------------
4647

47-
async function sendRequest(api, method, body) {
48-
try {
49-
const response = await fetch(serverUrl + api, {
50-
method: method || 'GET',
51-
headers: body ? { 'Content-Type': 'application/json' } : undefined,
52-
body
53-
});
54-
return await response.json();
55-
} catch (error) {
56-
return { error: error.message || 'Unknown error' };
57-
}
48+
function getAccounts() {
49+
return JSON.parse(localStorage.getItem(accountsKey) || '[]');
50+
}
51+
52+
function saveAccounts(accounts) {
53+
localStorage.setItem(accountsKey, JSON.stringify(accounts));
54+
}
55+
56+
function findAccount(user) {
57+
const accounts = getAccounts();
58+
return accounts.find(acc => acc.user === user) || null;
5859
}
5960

6061
async function getAccount(user) {
61-
return sendRequest('/accounts/' + encodeURIComponent(user));
62+
// Simulate async
63+
return new Promise(resolve => {
64+
setTimeout(() => {
65+
const acc = findAccount(user);
66+
if (!acc) resolve({ error: 'Account not found' });
67+
else resolve(acc);
68+
}, 100);
69+
});
6270
}
6371

64-
async function createAccount(account) {
65-
return sendRequest('/accounts', 'POST', account);
72+
async function createAccount(accountJson) {
73+
return new Promise(resolve => {
74+
setTimeout(() => {
75+
let data;
76+
try {
77+
data = JSON.parse(accountJson);
78+
} catch (e) {
79+
return resolve({ error: 'Malformed account data' });
80+
}
81+
if (!data.user) return resolve({ error: 'Username required' });
82+
if (findAccount(data.user)) return resolve({ error: 'User already exists' });
83+
// Set up initial account structure
84+
const newAcc = {
85+
user: data.user,
86+
description: data.description || '',
87+
balance: 0,
88+
currency: data.currency || 'USD',
89+
transactions: []
90+
};
91+
const accounts = getAccounts();
92+
accounts.push(newAcc);
93+
saveAccounts(accounts);
94+
resolve(newAcc);
95+
}, 100);
96+
});
6697
}
6798

68-
async function createTransaction(user, transaction) {
69-
return sendRequest('/accounts/' + user + '/transactions', 'POST', transaction);
99+
async function createTransaction(user, transactionJson) {
100+
return new Promise(resolve => {
101+
setTimeout(() => {
102+
const accounts = getAccounts();
103+
const idx = accounts.findIndex(acc => acc.user === user);
104+
if (idx === -1) return resolve({ error: 'Account not found' });
105+
const tx = JSON.parse(transactionJson);
106+
tx.amount = parseFloat(tx.amount);
107+
tx.date = tx.date || new Date().toISOString().slice(0, 10);
108+
accounts[idx].balance += tx.amount;
109+
accounts[idx].transactions.push(tx);
110+
saveAccounts(accounts);
111+
resolve(tx);
112+
}, 100);
113+
});
70114
}
71115

72116
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)