How can i avoid the don't repeat your self rule with this function

Barry B

He Developers,

I want to know your opinion about how to wright this code better.

If you click on button star3 it will turend as a yellow star stars underneath are also yellow. The star above star3 is just grey.

Thats what i got and what i need but i want to know how i can be more efficient in my code..

var star = document.getElementById("star-0");
 var star1 = document.getElementById("star-1");
 var star2 = document.getElementById("star-2");
 var star3 = document.getElementById("star-3");
 var star4 = document.getElementById("star-4");
 var img = 'url(img/star.png)';
 var img2 = 'url(img/star2.png)';


star4.addEventListener('click', function(){
   star.style.backgroundImage = img2;
   star1.style.backgroundImage = img2;
   star2.style.backgroundImage = img2;
   star3.style.backgroundImage = img2;
   star4.style.backgroundImage = img2;
});

star3.addEventListener('click', function(){
   star.style.backgroundImage = img2;
   star1.style.backgroundImage = img2;
   star2.style.backgroundImage = img2;
   star3.style.backgroundImage = img2;
   star4.style.backgroundImage = img;
});

star2.addEventListener('click', function(){
   star.style.backgroundImage = img2;
   star1.style.backgroundImage = img2;
   star2.style.backgroundImage = img2;
   star3.style.backgroundImage = img;
   star4.style.backgroundImage = img;
});

star1.addEventListener('click', function(){
   star.style.backgroundImage = img2;
   star1.style.backgroundImage = img2;
   star2.style.backgroundImage = img;
   star3.style.backgroundImage = img;
   star4.style.backgroundImage = img;
});

star.addEventListener('click', function(){
   star.style.backgroundImage = img2;
   star1.style.backgroundImage = img;
   star2.style.backgroundImage = img;
   star3.style.backgroundImage = img;
   star4.style.backgroundImage = img;
});

MaX

Seems like you are trying to implement rating stars. If you insist on only using JS not using any smarts or meta-data in HTML. Here is the function I would simplify it to:

var star = document.getElementById("star-0");
 var star1 = document.getElementById("star-1");
 var star2 = document.getElementById("star-2");
 var star3 = document.getElementById("star-3");
 var star4 = document.getElementById("star-4");
 var img = 'url(img/star.png)';
 var img2 = 'url(img/star2.png)';

function updateRating(rating) {
  var stars = [star, star1, star2, star3, star4];
  for(var i = 0; i < stars.length; i++) {
    var appliedImage = img;
    if ((i + 1) < rating) {
      appliedImage = img2;
    }
    
    stars[i].style.backgroundImage = appliedImage;
  }
}


star4.addEventListener('click', function(){
   updateRating(5);
});

star3.addEventListener('click', function(){
   updateRating(4);
});

star2.addEventListener('click', function(){
   updateRating(3);
});

star1.addEventListener('click', function(){
   updateRating(2);
});

star.addEventListener('click', function(){
   updateRating(1);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I repeat a function?

How can I avoid repetitive commands, such as cd in a target rule?

Some commands do run with PHP exec() others don't, how can I avoid/fix this?

I can't repeat a function in javascript

How can I avoid a memory leak in this function?

how can I change the function or avoid the error

How can I repeat a function and variable?

How can I make the function repeat?

In Firestore I can not create rule to avoid overwrite

Sort in reverse order. "Don't repeat yourself" rule

I don't know how to make this function

I don't understand the evaluation rule here

I don't know how i can run a function continuously on xamarin

How can i make a function out of this so i don't have to copy paste the same piece of code?

How can I work around (or with) this function returning hexadecimal characters when I don't want it to? (Python 3)

Don't understand php namespace, how can I call function from another class?

Can I avoid repeat calculation in this query?

Don't repeat yourself on Javascript function

Can I refactor to avoid "self::" and "parent::"?

AS3: How do I break up a function to avoid the 15 second time out rule?

How to make Class function repeat it self in python?

How can I run a function into an Object by your key name in TypeScript?

I don't know how to write a Rewrite Rule that involves a parameter in the original url (in my .htaccess)

How to form a TypeScript rule/config that errors if I don't return a JSX in React Functional Component

performing a merge function in python, when I don't want the values to repeat

How can I qualify an autowired setter that I don't "own"

how can i specify the column i don't want to display?

How can I work with vectors if I don't initialize it first?

Given a few facts in prolog. I can't seem to create a rule which returns the variables don't have a relationship with given variable