-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavascriptassignment.html
More file actions
60 lines (50 loc) · 2.01 KB
/
Javascriptassignment.html
File metadata and controls
60 lines (50 loc) · 2.01 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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS Objects</title>
</head>
<body>
<script>
//Create array of objects called Data with the following values:
let Data = [
{principal: 2500, time: 1.8 },
{principal: 1000, time: 5 },
{principal: 3000, time: 1 },
{principal: 2000, time: 3 }
];
//function called "interestCalculator" that takes an array as a single argument and does the following:
function interestCalculator(Data){
let interestData = [];
let _rate = 0;
let _interest = 0;
Data.forEach(array_element => { let _principal = array_element['principal'];
let _time = array_element['time'];
//If the principal is greater than or equal to 2500 and the time is greater than 1 and less than 3, then rate = 3
if (_principal >= 2500 && ( _time > 1 && _time< 3)){
_rate = 3;
}
//If the principal is greater than or equal to 2500 and the time is greater than or equal to 3, then rate = 4
else if (_principal >= 2500 && _time >= 3)
{
_rate = 4;
}
//If the principal is less than 2500 or the time is less than or equal to 1, then rate = 2
else if (_principal < 2500 || _time <= 1)
{
_rate = 2;
}
//Otherwise, rate = 1
else
{_rate = 1;}
//interest for each individual object
_interest = (_principal * _rate * _time) / 100;
interestData.push({principal : _principal, time : _time, rate : _rate, interest : _interest })});
console.log(interestData);
return interestData;
}
//Execute
interestCalculator(Data)
</script>
</body>
</html>