Jump to content

Short way of writing


aceuk007
 Share

Recommended Posts

Hi

Is there a shorter way of writing the following code.

(data === "item1" || data === "item2" || data === "item3" || data === "item4")

like

(data === "item1" to data === "item4")

as my list will be over 100 items.

Cheers

Heres a jsfiddle with the code in it http://jsfiddle.net/8mw1s2zj/6/

Please note this only has to work on IE11 and be jscript.

Here is the piece of code

 

var list=document.querySelectorAll("#dragsource li");
for(i=0;i<list.length;i++){
list.draggable=true;
list.ondragstart=function(event){
var event=event||window.event;
var dt=event.dataTransfer;
dt.setData("text", event.target.id);
dt.effectAllowed="move";
var data = dt.getData("text");
if((document.getElementById("onoff").value=="On") && (data === "item1" || data === "item2" || data === "item3" || data === "item4"))
{
(document.getElementById("fruit").style.color = "red") && (document.getElementById("veg").style.color = "black") && (document.getElementById("games").style.color = "black");
}
else if((document.getElementById("onoff").value=="On") && (data === "item5" || data === "item6" || data === "item7" || data === "item8")){
(document.getElementById("veg").style.color = "red") && (document.getElementById("fruit").style.color = "black") && (document.getElementById("games").style.color = "black");
}
else if((document.getElementById("onoff").value=="On") && (data === "item9" || data === "item10" || data === "item11" || data === "item12"))
{(document.getElementById("games").style.color = "red") && (document.getElementById("fruit").style.color = "black") && (document.getElementById("veg").style.color = "black");
}
else if((document.getElementById("onoff").value=="Off") && (data === "item1" || data === "item2" || data === "item3" || data === "item4" || data === "item5" || data === "item6" || data === "item7" || data === "item8" || data === "item9" || data === "item10" || data === "item11" || data === "item12"))
{
(document.getElementById("fruit").style.color = "black") && (document.getElementById("veg").style.color = "black") && (document.getElementById("games").style.color = "black");
}
};
}

 

 

Link to comment
Share on other sites

dsonesuk - My project is nearly finished. I was just looking to see if there was a shorter way of writing sections.What you are suggesting might be the correct way but seems like I would have to re-write a lot of stuff.

The problem with that is Most of the code is what I have picked up on the net and some help like yourself.I have no understanding of coding. So this is not something I would like to do.

cheers

Link to comment
Share on other sites

So basically you would like someone to provide the shortened code, and not just give explanation how it could be done. I've already done it! in last half hour, and it works brilliantly! shame I'm not going give you any help anymore, until you start to learn and attempt to make it yourself.

Link to comment
Share on other sites

You know I did not ask for you help you did that when you replied to my post.

Some people just don't grasp how this all works .I'm 57 and I'm not looking for a career in coding. I just wanted to create this small program.

You know it's sad that you wasted half hour of your life just so you can brag how great you are.

Am I bothered, not in the least. I will just stick with my not perfect but working code.

Link to comment
Share on other sites

Well, you know what they say. You can either take the time to do it right, or make the time to do it over. That applies in programming just as much as it would apply in carpentry, framing, automotive repair, or any other trade you can think of. Not to be Captain Hindsight or anything, but the time to get design help from programmers was when you were starting the application instead of finishing it. Using arrays is definitely the right way to design what you're trying to do. If you just want to shorten the amount of characters I suppose you can use an array to figure out if something is in the set, e.g.:

 

if (["item1", "item2", "item3", "item4", "item5", "item6"].indexOf(data) != -1)
That at least uses less characters but it's still not the cleanest thing in the world.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...