简体   繁体   English

JS星级评分系统

[英]JS star rating system

var num_stars_løn = 0
var num_stars_team = 0
var num_stars_trivsel = 0

function add_rating(num_rating, section) {
    if (section == "løn"){
        num_stars_løn = num_rating;
        document.getElementById("num_stars_løn").value = num_rating;
    }
    else if (section == "teambuilding"){
        num_stars_team = num_rating;
        document.getElementById("num_stars_team").value = num_rating;
    }
    else if (section == "trivsel") {
        num_stars_trivsel = num_rating;
        document.getElementById("num_stars_trivsel").value = num_rating;
    }

    for(var i = 1; i < num_rating + 1; i++) {
        var section_id =  section + " " + i
        document.getElementById(section_id).innerHTML = "&#x2605;"
    }
    if (num_rating < 5){
        for(var i = 5; i > num_rating; i--) {
            var section_id =  section + " " + i
            document.getElementById(section_id).innerHTML = "&#x2606;"
        }
    }


    if (num_stars_team > 0 && num_stars_løn > 0 && num_stars_trivsel > 0){
        show_mean_rating();
    }
}

function show_mean_rating() {
    var mean = document.getElementById("num_stars_løn").value + 
        document.getElementById("num_stars_team").value + 
        document.getElementById("num_stars_trivsel").value;
    document.getElementById("mean_rating").innerText = "Gennemsnitlig rating: " + (mean/3).toFixed(2);
}

HTML HTML

I have made a star rating system for some topics (3 in total) using JavaScript and HTML.我使用 JavaScript 和 HTML 为某些主题(共 3 个)制作了一个星级评分系统。 Do one of you know how to calculate the average/mean of how many stars each topic received?你们中的一个人知道如何计算每个主题获得多少颗星的平均值/平均值吗? So not the three topics all together, but the mean per topic of clicks/stars.所以不是三个主题放在一起,而是每个主题的点击次数/星级的平均值。

Right now it finds the mean of all 15 stars in total.现在它总共找到了所有 15 颗星的平均值。 (each topic has 5 stars) But it is supposed to calculate the mean for each individual topic. (每个主题有 5 颗星)但它应该计算每个单独主题的平均值。 I'm thinking maybe something with a counter for each meeting every time a user clicks on the stars, but I am not sure.我在想,每次用户点击星星时,每次会议都会有一个计数器,但我不确定。 And then maybe divide the stars with amount of votes/stars given然后可能会根据给定的票数/星星数来划分星星

To get the mean of each section, you would need to capture each vote with a counter (can be stored in state or in plain text with # of votes) and divide the sum of all of the votes by the number of votes.要获得每个部分的平均值,您需要使用计数器捕获每张投票(可以存储在状态或带有投票数的纯文本中)并将所有投票的总和除以投票数。

This can be done with an array of votes with each vote being added to an array every time a user clicks a rating, adding all the values in the array together, and dividing it by array.length.这可以通过投票数组来完成,每次用户点击评分时,每张投票都会添加到数组中,将数组中的所有值加在一起,然后除以 array.length。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM