-
-
Save noisy/11d19cdeb3ac399dffe4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head lang="pl"> | |
<meta charset="UTF-8"> | |
<script src="main.js"></script> | |
<title>most often</title> | |
</head> | |
<body> | |
<h2>Podaj elementy tablicy, oddzielone przecinkami</h2><input id="data"> | |
<button onclick="often()">Run</button> | |
<h2 id="view"></h2> | |
<h2 id="view2"></h2> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function often() { | |
var data = document.getElementById("data").value; | |
var array = data.split(","); | |
console.log(array); | |
var dominant = 0; | |
var dominant_counter = 0; | |
var grups = {}; | |
for (var i = 0; i < array.length; i++) { | |
if (grups[array[i]]) { | |
grups[array[i]] += 1; | |
} else { | |
grups[array[i]] = 1; | |
} | |
} | |
for (var n in grups) { | |
console.log(n); | |
console.log(grups[n]); | |
if (grups[n] > dominant_counter) { | |
dominant_counter = grups[n] | |
dominant = n; | |
} else if (grups[n] == dominant_counter) { | |
dominant = dominant +" " +n; | |
} | |
} | |
document.getElementById("view").innerHTML = (dominant_counter + " razy wystepuja elementy: " ); | |
document.getElementById("view2").innerHTML = (dominant); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment