Simple code for rating of users in JavaScript

Sameer Gurung
Fuzzy Code
Published in
1 min readJan 21

--

Here is an example of a simple code rating system implemented in JavaScript:

// An array to store the ratings for each user
let ratings = [];

// A function to add a new rating for a user
function addRating(user, rating) {
ratings.push({ user: user, rating: rating });
}

// A function to calculate the average rating for a user
function getAverageRating(user) {
let total = 0;
let count = 0;

for (let i = 0; i < ratings.length; i++) {
if (ratings[i].user === user) {
total += ratings[i].rating;
count++;
}
}

return total / count;
}

// Example usage
addRating("User 1", 4);
addRating("User 1", 5);
addRating("User 1", 3);
addRating("User 2", 2);
addRating("User 2", 4);
console.log(getAverageRating("User 1")); // 4
console.log(getAverageRating("User 2")); // 3

This is a very basic example, and in real-world scenario, you should consider security and validation before storing and displaying the data, also you can store the data in a database and use a web framework like express.js to handle the routing and data processing.

Please note that this is an example and it is not production ready and should not be used as it is.

--

--

Sameer Gurung
Fuzzy Code

A Software Engineer, who also turns to be a JavaScript enthusiast. Currently working with NodeJs, Angular, Ionic and AWS. Catch me: https://smrgrg.com