PHP Pharaoh
PHP Pharaoh

Reputation: 49

caculating the avarage from a list<String>

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

Answers (4)

Ibrahim Ali
Ibrahim Ali

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

Maxouille
Maxouille

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

M&#228;ddin
M&#228;ddin

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

Mayur Dhurpate
Mayur Dhurpate

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

Related Questions