Reputation: 49
suppose i have a list containing emojies:
List<String> _emojiesList = ['ā¹ļø','š','š','š²','š','š'];
let's say 6 people reviewed and selected an emoji from that list:
person 1 - ā¹ļø
person 2 - š
person 3 - š²
person 4 - ā¹ļø
person 5 - ā¹ļø
person 6 - š
im trying to display the avarage of those emojies.
i do know how to calculate the avarage:
avg = indexOfEmoji*numberOfSelected / totalNumberOfPeople
so:
ā¹ļø - 3
š - 2
š² - 1
(0 * 3 + 1 * 2 + 2 * 1) / 6 = 4/6 = 0.667.round() = 1
the problem is that the first emoji has been muntiplyed with index of 0.
it seems that can't find a way to solve this issue. I just need to print just the avarage.
Upvotes: 0
Views: 61
Reputation: 2503
After the user reviewed.. add index of the clicked emoji to a list.
[0, 1, 2, 3, 4, 5] // The list will look something like this
And then to calculate Average
var avg = ( reviewedList.reduce((a, b) => a + b) / reviewedList.length ).round();
print(_emojiesList[avg]);
Upvotes: 1
Reputation: 2911
I agree with @deeperm comment.
However, if you really want to do this, just offset indices by 1.
List<int> _emojiesNumbers = [1, 2, 4, 3, 5, 8];
void main() {
var avg = 0.0;
var idxByEmojies = 0;
var nbEmojies = 0;
_emojiesNumbers.asMap().forEach((index, value) {
idxByEmojies += (index+1)*value;
nbEmojies += value;
});
avg = idxByEmojies/nbEmojies;
print(avg);
}
Upvotes: 0
Reputation: 1319
It doesn't matter that the first emoji is multiplied by 0. Nevertheless, the correct result comes out, i.e. the average of the indices. It should fit that way.
If you want the first emoji to have weight 1, then all you have to do is increase the final score by 1.
Upvotes: 1
Reputation: 1282
I'm assuming that all 6 emojis have weights (or points) from 1 to 6.
In that case, you can increase the array index number by 1 while calculating the averages:
avg = (indexOfEmoji+1)*numberOfSelected / totalNumberOfPeople
In that case, you'll get,
(1 * 3 + 2 * 2 + 3 * 1) / 6 = 4/6 = 1.667
Upvotes: 1