Tuesday, 22 September 2015

Arithmetics2: find mode(number with the highest occurence) using Javascript.


Here's another one. What it does is it helps you find the mode(number with the highest ocurrence) in an array.

function sortproperly(a,b){
if(a>b) return 1;
else if(a<b) return -1;
else return 0;
}
Array.prototype.mode=function(){
document.write("<table border=1>")
for(i=1; i<=9; i++){
sot= this.sort(sortproperly).join(',')
r= new RegExp(i+"\,[\d\d\,]?","g")
u= sot.match(r)
if(!u){
continue;}
document.write("<tr><td>Number:"+i+"</td><td>Frequency:"+u.length+"</td></tr><br>")
}

document.write("<tr><td><p style='color:red;'>The Mode is the number with the highest frequency.</p></td></tr></table>")
}

m=[2,3,6,2,3,2,2,2,2]
m.mode()
The result will be:

Thanks. God bless.

No comments:

Post a Comment