Skip to content

Instantly share code, notes, and snippets.

@vale981
Created January 3, 2015 19:47
Show Gist options
  • Save vale981/0e18ada2099258510387 to your computer and use it in GitHub Desktop.
Save vale981/0e18ada2099258510387 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://www.chartjs.org/assets/Chart.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<input id="sample"></input>
<div id="result"></div>
<canvas id="income" width="600" height="400"></canvas>
<script id="jsbin-javascript">
var getOcc = function(stri){
var occs = {};
for (var i=0; i < stri.length; i++) {
var ch = stri.charAt(i);
if(ch != " "){
if (ch in occs)
occs[ch] += 1;
else
occs[ch] = 1;
}
}
//console.log(occs);
var k = Object.keys(occs).sort();
var soccs = {};
k.forEach(function(entr){
soccs[entr] = occs[entr];
});
return soccs;
};
var generateData = function(ob){
var tob = ob;
//console.log(tob);
var bd = {labels: [], datasets: [
{
fillColor : "#48A497",
strokeColor : "#48A4D1",
data : []
}
]};
for (var x in ob){
bd.labels.push(x);
bd.datasets[0].data.push(ob[x]);
}
return bd;
};
var compare = function(a,b) {
if (a.last_nom < b.last_nom)
return -1;
if (a.last_nom > b.last_nom)
return 1;
return 0;
};
var sortBV = function(originalData){
var dataArray = [];
for (var letter in originalData) {
var occ = originalData[letter];
dataArray.push({occ: parseInt(occ), letter: letter});
}
dataArray.sort(function(a, b){
if (a.occ > b.occ) return -1;
if (b.occ > a.occ) return 1;
return 0;
});
return dataArray;
};
var matchEng = function(ob, msg) {
var en = ['E','T','A','O','I','N','S','H','R','D','L','C','U','M','W','F','G','Y','P','B','V','K','J','X','Q','Z'];
var rep = {};
var obs = sortBV(ob);
var i = 0;
obs.forEach(function(x){
rep[x.letter] = en[i];
i+=1;
});
var dmsg = "";
for(var ch in msg){
//console.log(rep[msg[ch]]);
dmsg += rep[msg[ch]];
}
console.log(dmsg);
return 0;
};
$('#sample').keyup(function(){
var income = document.getElementById("income").getContext("2d");
var msg = $('#sample').val().toUpperCase();
var occ = getOcc(msg);
new Chart(income).Bar(generateData(occ));
matchEng(getOcc($('#sample').val().toUpperCase()), msg);
});
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery.min.js"><\/script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"><\/script>
<script src="//code.jquery.com/jquery-2.1.1.min.js"><\/script>
<script src="http://www.chartjs.org/assets/Chart.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<input id="sample"></input>
<div id="result"></div>
<canvas id="income" width="600" height="400"></canvas>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">
var getOcc = function(stri){
var occs = {};
for (var i=0; i < stri.length; i++) {
var ch = stri.charAt(i);
if(ch != " "){
if (ch in occs)
occs[ch] += 1;
else
occs[ch] = 1;
}
}
//console.log(occs);
var k = Object.keys(occs).sort();
var soccs = {};
k.forEach(function(entr){
soccs[entr] = occs[entr];
});
return soccs;
};
var generateData = function(ob){
var tob = ob;
//console.log(tob);
var bd = {labels: [], datasets: [
{
fillColor : "#48A497",
strokeColor : "#48A4D1",
data : []
}
]};
for (var x in ob){
bd.labels.push(x);
bd.datasets[0].data.push(ob[x]);
}
return bd;
};
var compare = function(a,b) {
if (a.last_nom < b.last_nom)
return -1;
if (a.last_nom > b.last_nom)
return 1;
return 0;
};
var sortBV = function(originalData){
var dataArray = [];
for (var letter in originalData) {
var occ = originalData[letter];
dataArray.push({occ: parseInt(occ), letter: letter});
}
dataArray.sort(function(a, b){
if (a.occ > b.occ) return -1;
if (b.occ > a.occ) return 1;
return 0;
});
return dataArray;
};
var matchEng = function(ob, msg) {
var en = ['E','T','A','O','I','N','S','H','R','D','L','C','U','M','W','F','G','Y','P','B','V','K','J','X','Q','Z'];
var rep = {};
var obs = sortBV(ob);
var i = 0;
obs.forEach(function(x){
rep[x.letter] = en[i];
i+=1;
});
var dmsg = "";
for(var ch in msg){
//console.log(rep[msg[ch]]);
dmsg += rep[msg[ch]];
}
console.log(dmsg);
return 0;
};
$('#sample').keyup(function(){
var income = document.getElementById("income").getContext("2d");
var msg = $('#sample').val().toUpperCase();
var occ = getOcc(msg);
new Chart(income).Bar(generateData(occ));
matchEng(getOcc($('#sample').val().toUpperCase()), msg);
});</script></body>
</html>
var getOcc = function(stri){
var occs = {};
for (var i=0; i < stri.length; i++) {
var ch = stri.charAt(i);
if(ch != " "){
if (ch in occs)
occs[ch] += 1;
else
occs[ch] = 1;
}
}
//console.log(occs);
var k = Object.keys(occs).sort();
var soccs = {};
k.forEach(function(entr){
soccs[entr] = occs[entr];
});
return soccs;
};
var generateData = function(ob){
var tob = ob;
//console.log(tob);
var bd = {labels: [], datasets: [
{
fillColor : "#48A497",
strokeColor : "#48A4D1",
data : []
}
]};
for (var x in ob){
bd.labels.push(x);
bd.datasets[0].data.push(ob[x]);
}
return bd;
};
var compare = function(a,b) {
if (a.last_nom < b.last_nom)
return -1;
if (a.last_nom > b.last_nom)
return 1;
return 0;
};
var sortBV = function(originalData){
var dataArray = [];
for (var letter in originalData) {
var occ = originalData[letter];
dataArray.push({occ: parseInt(occ), letter: letter});
}
dataArray.sort(function(a, b){
if (a.occ > b.occ) return -1;
if (b.occ > a.occ) return 1;
return 0;
});
return dataArray;
};
var matchEng = function(ob, msg) {
var en = ['E','T','A','O','I','N','S','H','R','D','L','C','U','M','W','F','G','Y','P','B','V','K','J','X','Q','Z'];
var rep = {};
var obs = sortBV(ob);
var i = 0;
obs.forEach(function(x){
rep[x.letter] = en[i];
i+=1;
});
var dmsg = "";
for(var ch in msg){
//console.log(rep[msg[ch]]);
dmsg += rep[msg[ch]];
}
console.log(dmsg);
return 0;
};
$('#sample').keyup(function(){
var income = document.getElementById("income").getContext("2d");
var msg = $('#sample').val().toUpperCase();
var occ = getOcc(msg);
new Chart(income).Bar(generateData(occ));
matchEng(getOcc($('#sample').val().toUpperCase()), msg);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment