Jump to content
aceuk007

Short way of writing

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");
}
};
}

 

 

Share this post


Link to post
Share on other sites

Create array of items with relation to specific box.

Loop though each box applying draggable, ondrop events compare with id ref in array and indexof() to see if current item relates to array list of items to allow drop.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...