How to calculate the Shannon Diversity Index?

@import url(‘https://fonts.googleapis.com/css?family=Droid+Serif|Raleway’);

h1 {
text-align: center;
font-size: 50px;
margin-bottom: 0px;
font-family: ‘Raleway’, serif;
}

p {
color: black;
margin-bottom: 15px;
margin-top: 15px;
font-family: ‘Raleway’, sans-serif;
}

#words {
padding-left: 30px;
color: black;
font-family: Raleway;
max-width: 550px;
margin: 25px auto;
line-height: 1.75;
}

#words_summary {
padding-left: 70px;
color: black;
font-family: Raleway;
max-width: 550px;
margin: 25px auto;
line-height: 1.75;
}

#words_text {
color: black;
font-family: Raleway;
max-width: 550px;
margin: 25px auto;
line-height: 1.75;
}

#words_text_area {
display:inline-block;
color: black;
font-family: Raleway;
max-width: 550px;
margin: 25px auto;
line-height: 1.75;
padding-left: 100px;
}

#calcTitle {
text-align: center;
font-size: 20px;
margin-bottom: 0px;
font-family: ‘Raleway’, serif;
}

#hr_top {
width: 30%;
margin-bottom: 0px;
border: none;
height: 2px;
color: black;
background-color: black;
}

#hr_bottom {
width: 30%;
margin-top: 15px;
border: none;
height: 2px;
color: black;
background-color: black;
}

#words label, input {
display: inline-block;
vertical-align: baseline;
width: 350px;
}

#button {
border: 1px solid;
border-radius: 10px;
margin-top: 20px;

cursor: pointer;
outline: none;
background-color: white;
color: black;
font-family: ‘Work Sans’, sans-serif;
border: 1px solid grey;
/* Green */
}

#button:hover {
background-color: #f6f6f6;
border: 1px solid black;
}

#words_table {
color: black;
font-family: Raleway;
max-width: 350px;
margin: 25px auto;
line-height: 1.75;
}

#summary_table {
color: black;
font-family: Raleway;
max-width: 550px;
margin: 25px auto;
line-height: 1.75;
padding-left: 20px;
}

.label_radio {
text-align: center;
}

td, tr, th {
border: 1px solid black;
}
table {
border-collapse: collapse;
}
td, th {
min-width: 50px;
height: 21px;
}
.label_radio {
text-align: center;
}

#text_area_input {
padding-left: 35%;
float: left;
}

svg:not(:root) {
overflow: visible;
}

td input {
max-width:80px;
max-height:30px;
}

The is a way to measure the diversity of species in a community.

To calculate this index for a given community, simply enter a list of observed frequencies for up to 10 species in the boxes below, then click the “Calculate” button:

Species Frequency
Species #1
Species #2
Species #3
Species #4
Species #5
Species #6
Species #7
Species #8
Species #9
Species #10

Shannon Diversity Index (H): 1.081384

Shannon Equitability Index (EH): 0.984318

function calc() {

//get input data
var o1 = +document.getElementById(‘o1’).value;
var o2 = +document.getElementById(‘o2’).value;
var o3 = +document.getElementById(‘o3’).value;
var o4 = +document.getElementById(‘o4’).value;
var o5 = +document.getElementById(‘o5’).value;
var o6 = +document.getElementById(‘o6’).value;
var o7 = +document.getElementById(‘o7’).value;
var o8 = +document.getElementById(‘o8’).value;
var o9 = +document.getElementById(‘o9’).value;
var o10 = +document.getElementById(‘o10’).value;

var obs = [o1, o2, o3, o4, o5, o6, o7, o8, o9, o10];
var empties = obs.filter(x => x==0).length;

var n = obs.reduce((a, b) => a + b, 0);

//do calculations
var diff1 = 0;
if (o1) {
diff1 = (o1/n)*Math.log(o1/n);
}
var diff2 = 0;
if (o2) {
diff2 = (o2/n)*Math.log(o2/n);
}
var diff3 = 0;
if (o3) {
diff3 = (o3/n)*Math.log(o3/n);
}
var diff4 = 0;
if (o4) {
diff4 = (o4/n)*Math.log(o4/n);
}
var diff5 = 0;
if (o5) {
diff5 = (o5/n)*Math.log(o5/n);
}
var diff6 = 0;
if (o6) {
diff6 = (o6/n)*Math.log(o6/n);
}
var diff7 = 0;
if (o7) {
diff7 = (o7/n)*Math.log(o7/n);
}
var diff8 = 0;
if (o8) {
diff8 = (o8/n)*Math.log(o8/n);
}
var diff9 = 0;
if (o9) {
diff9 = (o9/n)*Math.log(o9/n);
}
var diff10 = 0;
if (o10) {
diff10 = (o10/n)*Math.log(o10/n);
}

var errors = [diff1, diff2, diff3, diff4, diff5, diff6, diff7, diff8, diff9, diff10];
var H = -1*math.sum(errors);
var E = H / Math.log(obs.length-empties);

//output results
document.getElementById(‘H’).innerHTML = H.toFixed(6);
document.getElementById(‘E’).innerHTML = E.toFixed(6);

} //end calc function

x