...

View Full Version : if and



sherlockturtle
10-05-2011, 10:24 PM
I keep trying to make a if statment wiht two parameters but when i use && it does not work.

ironboy
10-05-2011, 10:29 PM
Please, clearify - show us the code! :)

sherlockturtle
10-05-2011, 10:38 PM
Please, clearify - show us the code! :)
It can just be a generic javascript if statment that has more than one parameter? How would you do that? i know theres a && "and" but it does not work.

ironboy
10-05-2011, 10:43 PM
var a = 1, b = 2;

// This will trigger an alert
if (a == 1 && b == 2){
alert("Yo! That's cool');
};

a=2;

// This won't
if (a == 1 && b == 2){
alert("Not!');
};

ironboy
10-05-2011, 10:45 PM
Which could also be written:

var a = 1, b = 2;

// This will trigger an alert
a == 1 && b == 2 && alert("Yo! That's cool');

a=2;

// This won't
a == 1 && b == 2 && alert("Not!');

DaveyErwin
10-05-2011, 10:45 PM
a=1;b=1;

if(a && b)
alert('both a and b are "truthy"')
else alert('either a or b or both are not "truthy"')

if(a==true && b==true)
alert('both a and b are "truthy"')
else alert('either a or b or both are not "truthy"')

ironboy
10-05-2011, 10:46 PM
Or like this:


var a = 1, b = 2;

// This will trigger an alert
a == 1 && b == 2 ? alert("Yo! That's cool') : 0;

a=2;

// This won't
a == 1 && b == 2 ? alert("Not!') : 0;

DaveyErwin
10-05-2011, 11:28 PM
if(1 & 3)alert("true")//alerts true
if(1 & 4)alert("true")//no alert

sherlockturtle
10-05-2011, 11:43 PM
Does it have to be a var can it be a input field value?

Old Pedant
10-06-2011, 01:02 AM
Of course it can be an input field value. It can be anything.

If you refuse to show us your code, we can't help you.

Old Pedant
10-06-2011, 01:04 AM
if(1 & 3)alert("true")//alerts true
if(1 & 4)alert("true")//no alert

Well, sure. But I thought this thread was about &&??

if(1 && 3)alert("true")//alerts true
if(1 && 4)alert("true")//also alerts true

sherlockturtle
10-06-2011, 03:03 AM
if (document.getElementById("name").value == "Timothy Conner" && document.getElementById("relatea").value == "William Conner"){
document.getElementById("relatec").value = "Brothers";
};
Well all of these are talking about input fields. It might not be the best explanation.

Old Pedant
10-06-2011, 04:41 AM
That looks perfectly okay to me.

Are you saying it doesn't work?

Then you will have to show the HTML page that it is working with.

devnull69
10-06-2011, 08:04 AM
Be careful with "dirty" user input! You should clean up the input before comparing it to any values using "=="

Use a custom trim() method to cut off leading and trailing white spaces, then use toLowerCase() or toUpperCase() to avoid case ambiguities.



function trim(myString) {
return myString.replace(/^\s+|\s+$/g, "");
}

var myName = trim(document.getElementById("name").value.toLowerCase());
if (myName == "timothy conner") ...


EDIT: didn't need the grouping parentheses in my regex

Philip M
10-06-2011, 08:27 AM
You might also wish to remove multiple spaces, and non-alpha characters except space hyphen apostrophe from the input. (myName = Mary-Lou O'Flanaghan).

sherlockturtle
10-06-2011, 11:27 PM
The how the realted part does not work.
<html>
<head>


<script>
function find(){
<!--name born age gender desc-->
var people = new Array
people [0] = ["Timothy Conner", "1999-Present", "Age: 12", "Male", ""]
people [1] = ["Andrew Conner", "1992-Present", "Age: 19", "Male", ""];

var how = new Array
how [0] = ["Brothers"]


if(document.getElementById("name").value == people [0] [0]){
document.getElementById("desc").value = people [0] [1] + " " + people [0] [2] + " " + people [0] [3];
}
if(document.getElementById("name").value == people [1] [0]){
document.getElementById("desc").value = people [1] [1] + " " + people [1] [2] + " " + people [1] [3];
}

document.getElementById("relateb").value = document.getElementById("name").value;
}
</script>
<script>
function related(){
if (document.getElementById("name").value == people [0] [0] && document.getElementById("relatea").value == people [1] [0]){
document.getElementById("relatec").value = how [0] [0];
};



</script>

<style>

</style>

</head>


<div id="main">

Name: <input type="text" id="name" value="" onChange="find()"/>
<br/>
<input type="text" value="" id="desc" size="200" style="background-color:transparent;border:0px solid white;" READONLY />
<br/>
<input type="hidden" value="" id="relate"/>
<br/>
<br/>
<br/>
<br/>
How is <input type="text" id="relateb"style="background-color:transparent;border:0px solid white;" READONLY /> related to:<input type="text" value="" id="relatea" onChange="related()"/>
<br/>
<input type="text" value="" id="relatec" size="200" style="background-color:transparent;border:0px solid white;" READONLY />
<br/>
</div>




</body>
</html>
This relate function does not work.

DaveyErwin
10-06-2011, 11:41 PM
You left out a }
and you need to make people global
I guess ?

try this way ...

<html>
<head>
<script>
var people = new Array;
function find(){
<!--name born age gender desc-->

people [0] = ["Timothy Conner", "1999-Present", "Age: 12", "Male", ""];
people [1] = ["Andrew Conner", "1992-Present", "Age: 19", "Male", ""];

var how = new Array;
how [0] = ["Brothers"];


if(document.getElementById("name").value == people [0] [0]){
document.getElementById("desc").value = people [0] [1] + " " + people [0] [2] + " " + people [0] [3];
}
if(document.getElementById("name").value == people [1] [0]){
document.getElementById("desc").value = people [1] [1] + " " + people [1] [2] + " " + people [1] [3];
}

document.getElementById("relateb").value = document.getElementById("name").value;
}
</script>
<script>
function related(){
if (document.getElementById("name").value == people [0] [0] && document.getElementById("relatea").value == people [1] [0]){
document.getElementById("relatec").value = how [0] [0];
}
};
</script>
<style>
</style>
</head>
<div id="main">
Name: <input type="text" id="name" value="" onChange="find()"/>
<br/>
<input type="text" value="" id="desc" size="200" style="background-color:transparent;border:0px solid white;" READONLY />
<br/>
<input type="hidden" value="" id="relate"/>
<br/>
<br/>
<br/>
<br/>
How is <input type="text" id="relateb"style="background-color:transparent;border:0px solid white;" READONLY /> related to:<input type="text" value="" id="relatea" onChange="related()"/>
<br/>
<input type="text" value="" id="relatec" size="200" style="background-color:transparent;border:0px solid white;" READONLY />
<br/>
</div>
</body>
</html>

Old Pedant
10-06-2011, 11:51 PM
Your problems have *NOTHING* to do with IF!

You need to learn how to DEBUG DEBUG DEBUG!

(1) You were missing a } after the end of your related function. YOUR JAVASCRIPT did not even COMPILE! So your related function could NEVER BE CALLED!

(2) You put the variables people and how *inside* the find function!!! That means they can *ONLY* be seen inside of that function. So the related function gives an error on even trying to *see* the variable people.

Both of those bugs took all of about 30 seconds to find using FIREBUG.

Learn to use Firebug! You will save yourself--and people in the forums--HOURS AND HOURS of frustration!

HERE:


<script>
var people = [
["Timothy Conner", "1999-Present", "Age: 12", "Male", ""],
["Andrew Conner", "1992-Present", "Age: 19", "Male", ""]
];

var how = [
"Brothers"
];

function find()
{
if(document.getElementById("name").value == people [0] [0])
{
document.getElementById("desc").value = people [0] [1] + " " + people [0] [2] + " " + people [0] [3];
}
if(document.getElementById("name").value == people [1] [0])
{
document.getElementById("desc").value = people [1] [1] + " " + people [1] [2] + " " + people [1] [3];
}
document.getElementById("relateb").value = document.getElementById("name").value;
}

function related()
{
if ( document.getElementById("name").value == people [0] [0]
&& document.getElementById("relatea").value == people [1] [0])
{
document.getElementById("relatec").value = how [0] [0];
}
}
</script>

Old Pedant
10-06-2011, 11:54 PM
Well, I'm a little slower than Davey, but I got to the same place.

Sherlock: Next time, SHOW YOUR CODE in your FIRST post, instead of making us drag it out of you, okay?

sherlockturtle
10-07-2011, 12:57 AM
THanks guys. WHat is fire bug.

Old Pedant
10-07-2011, 02:58 AM
FIREBUG is a free plugin for the FIREFOX browser.

Download the Firefox browser, first, and install and run it.

Click on the TOOLS menu. Click on the ADDONS menu item. Click on GET ADDONS. Find and install FIREBUG.

Simple as that. But then you need to learn to *USE* it.

sherlockturtle
10-07-2011, 01:10 PM
FIREBUG is a free plugin for the FIREFOX browser.

Download the Firefox browser, first, and install and run it.

Click on the TOOLS menu. Click on the ADDONS menu item. Click on GET ADDONS. Find and install FIREBUG.

Simple as that. But then you need to learn to *USE* it.

thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum