After losing the lotto tonight I decided I was going to try and see if picking the most common numbers people win with gets me more numbers hit in future games.
So I wrote a js
function to run through a multi dimensional array and count the number of occurrences each number appears. This way in the future I can add to the array and see what the top 6 most picked numbers are each week.
"I realize I am not going to win the lottery like this, I'm a nerd and this is how I enjoy myself."
At this point I have an object
literal and read that it is best to convert to json
before sorting because it is not accurate.
So I used,
JSON.stringify(obj)
My program shows me the each number in my multidimensional array and then shows me how many times it has been mentioned.
At this point my json output looks like this,
{"1":6,"2":6,"3":6,"4":6,"5":5,"6":6,"7":1}
All of the examples I see to sort json have json that looks like this,
"zip": "00010",
"price": "962500"
With something like that I could do this to sort by price,
numbersCalced.sort(function(a, b) {
return a.price - b.price;
});
But in my case my goal is to show the top 6 largest numbers. So I can't just sort by any number between 1 and 49. How can I sort this json not by the number but by the amount it occurred.
At that point it will be easy to move the top 6 numbers to an output.
{"1":6,"2":6,"3":6,"4":6,"5":5,"6":6,"7":1}
Here is the full script if it helps,
var arr = [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 7, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6]
];
// 49 numbers is the real game
var obj = {};
for (var g = 0; g < arr.length; g++) {
for (var i = 0, j = arr.length; i < j; i++) {
if (obj[arr[g][i]]) {
obj[arr[g][i]]++;
} else {
obj[arr[g][i]] = 1;
}
}
}
var numbersCalced = JSON.stringify(obj)
console.log(numbersCalced);
TO BE CLEAR it is sorting it as of now. Putting it in order of the number not the occurrence.
so 1 occurrences 7 will always be in the 0 place. How can I show the order based on the occurrences?