Skip to content

Instantly share code, notes, and snippets.

@yangli-io
Created November 8, 2015 09:31
Show Gist options
  • Select an option

  • Save yangli-io/e25a8c0c5fcdb068ee40 to your computer and use it in GitHub Desktop.

Select an option

Save yangli-io/e25a8c0c5fcdb068ee40 to your computer and use it in GitHub Desktop.
var images = ['images/food1.jpg', 'images/food2.jpg', 'images/food3.jpg'];
var votes = [0, 0, 0]; //This is the average
var vote;
var imageNum = 0;
var average
$('#back').click(function(){
if (imageNum !== 0){
imageNum --;
} else {
imageNum = 2;
}
$('#your-vote').val(votes[imageNum])
$('img').attr("src", images[imageNum])
});
$('#skip').click(function(){
if (imageNum !== 2){
imageNum++;
} else {
imageNum = 0;
}
$('#your-vote').val(votes[imageNum])
$('img').attr("src", images[imageNum])
});
$('#your-vote').change(function(){
vote = $('#your-vote').val() * 1;
votes[imageNum] = vote;
average = findAvg(votes);
})
function findAvg(votes){
var avg;
var sum = 0;
for (var i = 0; i < votes.length; i++){
sum += votes[i];
}
avg = sum / votes.length;
return avg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment