Help with js logic - if several ifs are not the case do something else - how to?

I really need your help here I can’t figure out the logic here.

I have something like:

if (inputFieldIsThere){

if (inputField="someStringX" || inputField="someStringY") 
{
 doSomething;
} 

if (inputField="someStringZ" || inputField="someStringT")
{
 doSomething;
}


}//end of main conditional

What I would like to be able to do is, on this structure add:
If the string on the input field is different from any of the options that are there inside the different conditionals, do something else.

I cannot put the else after if(inputField=“someStringz” …) because, is someone does “someStringX” (on the other conditional), it will trigger, and that is not intended.

I cannot put the else on the main conditional, because I don’t what this to happen if the input field isn’t there.

Is this clear? At least more or less clear?

How/where should we write a conditional that says: "If any other text (that is not covered by any of those conditionals) is on the input field, then, do something?

Please PLEASE, help me out here, I’m on circles now, :sick:

Márcio

You can use else if


if (inputFieldIsThere) {
    if (inputField === "someStringX" || inputField === "someStringY") {

    } else if (inputField === "someStringZ" || inputField === "someStringT") {

    } else {

    }
}

Here is the pastebin of what I have:
http://pastebin.com/zd5Vjead

I’m feeling bad with all those lines and the fact that I cannot understand faster a quicker and simply way of doing the same thing, still, the quest is know, properly understand the else if.

So the problem is, I already have an else (that probably shouldn’t be there), on those ifs that I should use the else if, hence, I believe it will not work?

Thanks a lot,
Márcio

You seem to be defining a switch

if(inputFieldIsThere){
	switch(inputField){
		case "someStringX": case "someStringY": 
			doSomething;
			break;
		case "someStringZ": case "someStringT": 
			doSomething;
			break;
		default: 
			dosomething
	}
}

I do believed that also, I have tried the switch but the end question was the same:

how can I “grab” several cases and say, if any of those cases “aren’t the case” do something ?

Is the default the answer? Is the default that will do:
“if any of those cases fit, do this?”

Please keep on replying I will give a quick try…

Update: But I will need, at least on the first case, to have a case inside a case? (can we have it?)

Thanks a lot,
Márcio

Aside from the problem at hand, you can use a regular expression to check that the string starts with an optional period, followed by the tld you desire.

For example:


function  isTLD(value, name) {
    name = name.toLowerCase();
    var tldRe = new RegExp('^\\.?' + name + '$');
    return tldRe.test(value);
}
...
if (isTLD(document.getElementById("outroTld").value, 'com')) {
    ...
} else {
    ...
}

About doing something if nothing matches, you could scan through document.dados.dominio_freq0 and the others checking if any are disabled. If they are then that would be the condition that you’re looking for.


var someFreqEnabled = false;
var index = 0;
var el = document.dados['dominio_freq' + index];
while (el) {
    if (el.disabled === true) {
        someFreqEnabled = true;
    }
    el = document.dados['dominio_freq' + index];
    index += 1;
}
if (someFreqEnabled) {
    ...
}

I can’t. This code was provided with some weird stuff, and the . is not on the tld is, after the domain name. and it comes from server side languages… and… Ow… is just a mess. And the worst, is that I’m not good enough to make it better.

Let’s forget it for now. But thanks really. :slight_smile:

According to the code on the pastebin above, where should I place this while?

If we had only a flag that stays false and true, and then, at the end of all the if statements we had: “if that flag==false” to something. THIS will not work I suppose, and that’s why we need a while?

Boolean answers will suffice, I’m feeling very lost already. :s

thanks, and please have patience,
Márcio

pastebin won’t load for me at the moment, (though it did before) but the place to put the while could be after line 304.

That’s another way, where you set the flag to false at the start, and then within each else condition set the flag to true, so that at the end of things checking if the flag is true.

That’s one way, but to me it smells because you’re mixing it in with the other conditional code.

The other way that I chose was to wait until the end of things, and then look for changes that each else condition performs.

You still have the same boolean at the end of things, (someFreqOccurred) regardless of which approach is taken.

Thanks.

Here is another paste bin with the while included.

I have put an alert that I was hoping to popup, if the text were something different then .com, .net, etc… but to not popup when we write .com, .net etc… on the outroTld input field.

What’s happening now:
If I write something else outside the “scope” of that words, I get the alert. Fine.
If we write one of those words, I also get the alert. Not fine.

Could this be related with the while placement?

Thanks a lot,
Márcio

AHhhhHHH !

Ok, the dummy way worked, I have ignored the other variable (the freq0 one),
and I have created a new flag.

However, I have ignored the while instruction and I have made it the dummy way: if blabla1 && blabla2 && blabla3 …

I prefer to have a while instead.

Can I have your help on implementing a while instead of having that long conditional?

The paste bin is (update:NOT) the same, the line in question is:

http://pastebin.com/ZA2AEXtR

line
278

Better yet, I will try to do a while or a for and if I get an error I will post back.

Thank you really for the big push!
Márcio

Actually, is not that simple as I thought it could be, where should we put something like this:

If we try something like:

  
for (i=0; i<=7; i++)
                        {
                            if(naoEscreveuPalavraChave+i == true)
                            {
                                document.getElementById("spanJaRegistado").style.display="";
                                document.getElementById("dominio3").style.display="none";
                                document.getElementById("dominio_anos").style.display="none";
                            }
                        }

If we do like this, we should declare yet another variable,
the naoEscreveuPalavraChave (without any numbers in front)?

Is this a proper way for dealing with the long conditional on the if clause?

The while loop would be a very similar construct.


var someAreTrue = false;
var index = 0;
var value = this.['naoEscreveuPalavraChave' + index];
while (value) {
    if (value === true) {
        someAreTrue = true;
    }
    index += 1;
    value = this.['naoEscreveuPalavraChave' + index];
}
if (someAreTrue) {
    ...
}

Thanks.

I have implemented here:

http://pastebin.com/VxkBTB78

From line:
278

to

299

I only removed the (concatenation?) . after the this (I need to search about what the this do).

But I don’t get the same results as if I do with that bunch of ANDs :s

Could the fact that I cannot apply this while, be related with the fact that upper on the code I have
naoEscreveuPalavraChave0;
naoEscreveuPalavraChave1;
naoEscreveuPalavraChave2;

and not something like:
naoEscreveuPalavraChave+index;

But if need to have this, that the index declaration and the while should surround all the if structures yes?

What am I missing?

Thanks a lot,
Márcio

ps- please note, the issue is solved, I’m only trying to understand how can we pass from a lot of ands to a much proper way of doing it, that is using a while.

Back in 1 hours.

That seems to be the problem right there.

Ow… ok… please have patience, I have remove it, because, if I don’t remove it I get a syntax error on my IDE - “missing name after . operator”.

:shifty:

Oh yes, please get rid of that fullstop and use this[‘naoEscreveuPalavraChave’ + index];

And I have, on the last paste bin is exactly what I have but, I’m unable to get the same results, as using a bunch of Ands.

But this code is messy, I must reserve the teachings for code that I can properly control, probably I have some issues somewhere else and, probably that’s also why the while doesn’t work here.

No problems, I’m just super glad that our discussion lead me to a solution for this problem. I believe I will have a lot of more other js problems to solve.

Thanks a lot,
Márcio

I’m more awake now, and realise that we can make it work when we can access the function variables in an array-like manner, or if the variables are stored in an array or an object.

Does anyone recall how to access other variables within the same function, using variable variables?

I am tempted to otherwise scratch the whole idea in favour of array-like storage instead.

Believe it or not, I’ve thought on an array approach, the only think that has keep me out of doing it was that I didn’t needed to store each and all values, I just needed to run something if a couple of values where something. I mean, we need to store each value, of course, but it was so temporary that I was wondering if the array could be the proper solution for doing so.

But I’m really glad that I’m start thinking on “array” and “conditionals” and for or whiles for solutions. :slight_smile: I’m a happy newbie, but it won’t last.

Anyway, no worries about that, the script will not change that often (hopefully), hence, the 7 ANDs are ok for me at this moment and for a long time, at least, until a all new form is made and I can then stop “fixing” this one.

Of course, this would be (update)possible without your and others help here on site point. And I’m really serious here. For solo development, and when you have no programmer real friends, where can you rely on? Not all questions (and most of all, the logical ones, or the best practice ones) appear on programming books. They mostly talk about sintax, what something do or not do,but they cannot provide all. I’m just glad as a Sitepoint addict. eheeh :slight_smile:

Ok… I cannot write proper english, but that doesn’t stop me hm? Well, if anyone else would like to enrich this thread, perfect, if not, for me, I’m more then glad at this moment. :slight_smile:

Regards,
Márcio