-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment16.js
158 lines (130 loc) · 4.31 KB
/
assignment16.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
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
// function getStringKeys(obj) {
// let stringKeys= Object.keys(obj).filter(key => typeof obj[key] === 'string');
// return stringKeys
// }
// const input={name:"John",age:25,city:"New York",occupation:42};
// console.log(getStringKeys(input));
// function swapKeysAndValues(obj) {
// const swappedObj = {};
// for (let key in obj) {
// if (obj.hasOwnProperty(key)) {
// swappedObj[obj[key]] = key;
// }
// }
// return swappedObj;
// }
// const input={a:1,b:2,c:3}
// console.log(swapKeysAndValues(input))
function createBankAccount(initialBalance) {
let balance = initialBalance;
return {
deposit: function(amount) {
if (amount > 0) {
balance += amount;
return `Deposited: $${amount}. New balance: $${balance}`;
} else {
return "Deposit amount must be greater than zero.";
}
return;
},
withdraw: function(amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return `Withdrew: $${amount}. Remaining balance: $${balance}`;
} else if (amount > balance) {
return "Insufficient balance.";
} else {
return "Withdrawal amount must be greater than zero.";
}
return;
},
checkBalance: function() {
return `Current balance: $${balance}`;
}
};
}
const myAccount = createBankAccount(200);
console.log(myAccount.deposit(50));
console.log(myAccount.withdraw(30));
console.log(myAccount.checkBalance());
console.log(myAccount.withdraw(200));
// function sumToSingleDigits(num) {
// while (num >= 10) {
// let digits = num.toString().split('');
// let sum = 0;
// for (let digit of digits) {
// sum += Number(digit);
// }
// num = sum;
// }
// return num;
// }
// console.log(sumToSingleDigits(12345))
// console.log(sumToSingleDigits(987))
// console.log(sumToSingleDigits(9999))
// console.log(sumToSingleDigits(5))
// function getStringKeys(obj){
// let string_keys=Object.keys(obj).filter(key=>typeof obj[key]==="string")
// return string_keys
// }
// const input={name:"John",age:25,city:"New York",occupation:42}
// / / console.log(getStringKeys(input))
// function swapKeysAndValues(obj){
// const swappedObj={};
// for(let key in obj){
// if(obj.hasOwnProperty(key)){
// swappedObj[obj[key]]=key
// }
// }return swappedObj
// }
// const input={1:"a",2:"b",3:"c"}
// console.log(swapKeysAndValues(input))
// function createBankAccount(initialBalance){
// let balance=initialBalance
// return{
// deposit:function(amount){
// if(amount>0){
// balance+=amount
// return `Deposited: ${amount}. New balance:${balance}`
// }
// else {
// return `Deposit amount must be greater than zero`
// }
// return;
// },
// withdraw:function(amount){
// if(amount>0&&amount<=balance){
// balance-=amount
// return `Withdraw: ${amount}. Remaining balance: ${balance}.`
// }
// else if(amount>balance){
// return `Insufficient balance`
// }
// else{ return `Withdrawal amount must be greater than zero`}
// return;
// },
// checkBalance:function(){
// return `Current balance: ${balance}`
// }
// }}
// const account1=createBankAccount(200)
// console.log(account1.deposit(50))
// console.log(account1.withdraw(30))
// console.log(account1.checkBalance())
// console.log(account1.withdraw(200))
// const account2=createBankAccount(500)
// console.log(account2.deposit(80))
// console.log(account2.withdraw(20))
// console.log(account2.checkBalance())
// console.log(account2.withdraw(600))
// function sumToSingleDigits(num){
// while (num>=10){
// let digits =num.toString().split('')
// let sum =0
// for (let digit of digits){
// sum+=Number(digit)
// }
// num=sum}
// return num
// }
// console.log(sumToSingleDigits(1,2,3,4,5))