I have the following problem.
I have a page built with (html and javascript) that contains 4 checkboxes(lunch, diner, meet, rest), one input field(placename) and a go to next page button. These checkboxes and input field are all optional, they can also be left blank/unselected.
If I click the button and go to the next page there will be, genereted from javascript, a list that comes from a hardcoded json:
var myData = [ {
"Name" : "Bla",
"Placename" : "Amsterdam",
"lunch" : "true"
},
{
"Name" : "Bla2",
"Placename" : "Paris",
"lunch" : "false",
"diner" : "true"
},
{
"Name" : "Bla3",
"Placename" : "London",
"meet" : "false",
"diner" : "true"
},
{
"Name" : "Bla4",
"Placename" : "Berlin"
}];
The idea now is that I check which checkbox is selected and/or a placename is inserted I do this as follows:
if(placename == ""){
check json without checking placename..
}else{
if( ((lunchCheckbox == checked) && (element.lunch == "true")) && (Placename == element.Place)){ }
But the problem here is that now there will be many if statements, is there a way to do this better(with less statements)..
I thought about a loop but want to know if there are better ways
I think this will solve the problem:
If you have forexample four checkboxes called: A,B,C,D. Then a solution would be to make bits of these.. so for example A = 0 or 1 B = 0 or 1 etcetera.. Then combine these bits with eachother and then you get like 0000 which would mean ABCD are all false, or 0001 only D is true. Afterwards you could save this as a string like TotalBits = “0000”. Then you do this also with the json parameters lunch=”true” = 1 and save the total bits in TotalBits2 for example, at the end you check if the bits are the same and here you go 🙂
so for example if you code it…
Then the same for the lunch, for example:
in the end check if totalBits1 == totalBits2 then produce result ..