Skip to content

Commit c01047a

Browse files
calculate rms
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 3af63eb commit c01047a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Lecture08/array-ops/calcrms.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
let calc = document.getElementById('calc')
2+
let vals = document.getElementById('vals')
3+
let nRMS = document.getElementById('nRMS')
4+
let pRMS = document.getElementById('pRMS')
5+
let RMS = document.getElementById('RMS')
6+
7+
calc.onclick = function() {
8+
let numArr = vals.value.split(',').map((i) => +i.trim())
9+
let meanSqr = (ac, val, i, a) => ac + (Math.pow(val, 2) / a.length)
10+
11+
RMS.innerText = Math.sqrt(
12+
numArr
13+
.reduce(meanSqr, 0)
14+
)
15+
pRMS.innerText = Math.sqrt(
16+
numArr
17+
.filter((i) => i > 0)
18+
.reduce(meanSqr, 0)
19+
)
20+
nRMS.innerText = Math.sqrt(
21+
numArr
22+
.filter((i) => i < 0)
23+
.reduce(meanSqr, 0)
24+
)
25+
}

Lecture08/array-ops/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="X-UA-Compatible" content="ie=edge">
77
<title>Document</title>
8-
<script src="array-ops.js"></script>
8+
<script defer src="calcrms.js"></script>
99
</head>
1010
<body>
1111

0 commit comments

Comments
 (0)