-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS1101S R8.js
39 lines (36 loc) · 1.25 KB
/
CS1101S R8.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
//Q1
function make_withdraw(balance,password) {
function withdraw(amount,pass) {
if (account_disable === 1) {
return 'Account disabled';
} else if (pass !== password) {
attempts = attempts + 1;
display(attempts);
if (attempts > 2) {
account_disable = 1;
return 'Wrong password; no withdraw';
} else {
return 'Wrong password; no withdraw';
}
} else if (balance >= amount) {
attempts = 0;
balance = balance - amount;
return balance;
} else {
attempts = 0;
return "Insufficient funds";
}
}
let attempts = 0;
let account_disable = 0;
return withdraw;
}
const acc = make_withdraw(100,'my_password');
acc(30, "his_passcode"); // returns "Wrong password; no withdraw"
acc(30, "my_password"); // returns 70
acc(10, "sesame"); // returns "Wrong password; no withdraw"
acc(15, "canola"); // returns "Wrong password; no withdraw"
acc(25, "olive"); // returns "Wrong password; no withdraw"
acc(30, "my_password"); // returns "Account disabled"
acc(30, "his_passcode"); // returns "Account disabled"
// Q2