• 0

logon function


Question

I am at my wits end why this wont work any help would be appreciated

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>Untitled Document</title>
<script>
function logon()
{var userid
 var password
 var valid
 userid=prompt('enter userid','')
 password=prompt('enter password','')
 valid=validatelogon(userid,password)
 if(valid==true)
 {
 alert('valid logon')
 }
 else
 {
 alert('invalid logon')
 
 }
 function validatelogon(id,pwd)
 {
 var Returnvalue
 if(id=='peter' && pwd =='tree')
 {
 Returnvalue=true
 }
 else
 {
 Returnvalue=false
 }
 return Returnvalue
 
 
 }
}
</script>
</head>
 
<body>
Logon()
 
</body>
</html>
 
Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

Hello,

 

As a personal suggestion always state the type of script:

 

<script type="text/javascript>

 

Im not sure if thats standard compliant but it makes the code a bit easier.

 

Im gonna try out your code...



Use ;

I looked at script type not being stated and didnt even look that it had a bunch of ";" missings :laugh:
Link to comment
Share on other sites

  • 0

 

Hello,

 

As a personal suggestion always state the type of script:

<script type="text/javascript>

Im not sure if thats standard compliant but it makes the code a bit easier.

 

Im gonna try out your code...

I looked at script type not being stated and didnt even look that it had a bunch of ";" missings :laugh:

 

As of html 5 a script declaration is unnecessary. For some reason, the op is using xhtml which should use it for standards but should still work. 

 

Whats wrong here:

Case sensitivity: you name your function logon, and then call the function Logon that doesnt exist.

Function outside script tags. Why are you just calling "logon" in the body? You need to wrap all javascript in script tags (apart from some cases like forms).

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<script>
function logon() {
	var userid;
 	var password;
 	var valid
 	
 	userid=prompt('enter userid','');
 	password=prompt('enter password','');
 	valid=validatelogon(userid,password);
 
 	if(valid==true) {
 		alert('valid logon');
 	} else {
 		alert('invalid logon');
 	}
 
function validatelogon(id,pwd) {
 	var Returnvalue;
 	if(id=='peter' && pwd =='tree') {
 		Returnvalue=true;
 	} else {
 		Returnvalue=false;
 	}
 return Returnvalue;
}

}
</script>
</head>
 
<body>
<script>
logon();
</script>
 
</body>
</html>
 
Link to comment
Share on other sites

  • 0

Hello,

As of html 5 a script declaration is unnecessary.

Ah OK, thats why I wasnt sure if it was standard or optional now. Thank you.

 

Whats wrong here:

Case sensitivity: you name your function logon, and then call the function Logon that doesnt exist.

Function outside script tags. Why are you just calling "logon" in the body? You need to wrap all javascript in script tags (apart from some cases like forms).

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<script>
function logon() {
	var userid;
 	var password;
 	var valid
 	
 	userid=prompt('enter userid','');
 	password=prompt('enter password','');
 	valid=validatelogon(userid,password);
 
 	if(valid==true) {
 		alert('valid logon');
 	} else {
 		alert('invalid logon');
 	}
 
function validatelogon(id,pwd) {
 	var Returnvalue;
 	if(id=='peter' && pwd =='tree') {
 		Returnvalue=true;
 	} else {
 		Returnvalue=false;
 	}
 return Returnvalue;
}

}
</script>
</head>
 
<body>
<script>
logon();
</script>
 
</body>
</html>
 
Careful, I got almost warned for posting the complete code solution to a problem and my code was removed :(

I tried his code on my PC and everything you said and Seahorsepip is basically what is missing/wrong.

Link to comment
Share on other sites

  • 0
Oh and just some stuff everybody keeps doing wrong....



var auto = 0;
if(auto){
    alert("yes");
}
if(!auto){
    alert("no");
}


 

is simpler code for the commonly used:

 



if(auto==true){
    alert("yes");
}
if(auto==false){
    alert("no");
}


 

Keep in mind true and false aren't the only values that work:

 

In above code the following values return true:



auto = true;
auto = 1;
auto = "Any sort of text";
auto = some number;


 

In above code the following values return false:



auto = false;
auto = 0;
auto = "";


auto = null ;//Can be used to remove a value from a veriable
auto = undefined; //An error you sometimes get when you forget to set a value
auto = NaN; //Same message you sometimes get when a functions returns nonthing


 

I recommend to use 1 and 0 since it's less bothersome to type and shorter :P

 

Though you can use all of them, except the last 3 values for false, those should be used when you actually know more about them and when they should be used.

 

Oh and never forget to put ; behind a function or setting a variable, examples:

 



heythere();
auto = 0;


 

And know the difference between the following 2 things:



var auto = 0;
auto = 0;


 

In the first case you CREATE a new variable and in the second case you MODIFY a already existing variable.

 

And variables can be set inside functions and outside them as example for settings them inside:

 



function count(){
    var auto = 5;
    if(auto==5){
        alert("Auto is 5");
    }
}
function car(){
    var auto = 5;
    if(auto<6){
        alert("You got less then 6 cars, to be exact you got "+auto+" cars.");
    }
}


 

The above code sets the variable auto twice which is unnecessary, instead write the variable above the functions:

 



var auto = 5;
function count(){
    if(auto==5){
        alert("Auto is 5");
    }
}
function car(){
    if(auto<6){
        alert("You got less then 6 cars, to be exact you got "+auto+" cars.");
    }
}


 

Then you can use 'auto = 4;' to change the global variable auto inside the functions, now you also don't have to use 'var' anymore since you're modifying a already existing value.

 

Also this prevents unnecessary passing of variables trough functions since all functions can access this variable now.

 

Also know when to use "" and when not, when you're working with the booleans(false or true) you don't need to use them and also when working with numbers but when you're working with text never forget to use them.

 

Some tricky things that clear up the importance of using "" when necessary or when not:

 



auto = "5";
car = 3;


verhicles = auto+car; //in this case verhicles will give "53"...


pink = 5;
red = 3;


colors = pink+red; //in this case colors will give 8


 

Also when writing comments inside code you can use:

 



// for 1 line comments


 

and

 



/*
Multiple
lines
of
comments
*/


 

I guess this is enough for today's lessons :P

Link to comment
Share on other sites

  • 0

Hello,

Oh and just some stuff everybody keeps doing wrong....

var auto = 0;
if(auto){
    alert("yes");
}
if(!auto){
    alert("no");
}

is better code for the commonly used:

if(auto==true){
    alert("yes");
}
if(auto==false){
    alert("no");
}
I, for legacy reasons, disagree.

I personally:

if(auto==true){
    alert("yes");
}
else if(auto==false){
    alert("no");
}
else{
    alert("something else");
}
Yup, its more code and Im sure your method is more efficient but I guess Im old school for some things :p
Link to comment
Share on other sites

  • 0

Hello,

I, for legacy reasons, disagree.

I personally:

 

if(auto==true){
    alert("yes");
}
else if(auto==false){
    alert("no");
}
else{
    alert("something else");
}
Yup, its more code and Im sure your method is more efficient but I guess Im old school for some things :p

 

Haha yeah I also used to write true and false everytime but It's just so bothersome to keep writing that xD

 

Oh and a last thing people tend to forget or don't even know:

auto = 5;
color = 5;
red = 5;
blue = 5;
windows = 5;

can also be written as:

auto = color = red = blue = windows = 5;

This way you can set multiple variables at once easier ^^

Link to comment
Share on other sites

  • 0

Hello,

Haha yeah I also used to write true and false everytime but It's just so bothersome to keep writing that xD

Yeah, thats understandable :) I like code that is explicit, but thats a personal thing and your code is better.
 

Oh and a last thing people tend to forget or don't even know:

auto = 5;
color = 5;
red = 5;
blue = 5;
windows = 5;
can also be written as:
auto = color = red = blue = windows = 5;
This way you can set multiple variables at once easier ^^

 

Yup; This "feature" is mostly supported by scripting languages (some high level programming languages might support it to, IDR)

Link to comment
Share on other sites

  • 0

Hello,

Yeah, thats understandable :) I like code that is explicit, but thats a personal thing and your code is better.

 

Yup; This "feature" is mostly supported by scripting languages (some high level programming languages might support it to, IDR)

What feature? Isn't that like just a logical expression that can be used by any high level language?

Link to comment
Share on other sites

  • 0

Hello,

What feature? Isn't that like just a logical expression that can be used by any high level language?

Nope. It doesnt work in every single high level language.
Link to comment
Share on other sites

  • 0

Hello,

It works in C so other high level languages should be ashamed of themselves...

Personally, its a feature I dont use much but Reading around not all languages have it. Most do :)
Link to comment
Share on other sites

  • 0

It works in C so other high level languages should be ashamed of themselves...

 

Depends, some languages don't support it but support a other syntax instead:

auto, color, verhicle, motor = 5;

Also some languages have int and String instead of var and other types of syntax like $variable, so for each language you start working with you have to look again into the syntax  ^^

Link to comment
Share on other sites

  • 0

Depends, some languages don't support it but support a other syntax instead:

auto, color, verhicle, motor = 5;

Also some languages have int and String instead of var and other types of syntax like $variable, so for each language you start working with you have to look again into the syntax  ^^

Yes, I believe everybody here is aware of that.

Link to comment
Share on other sites

  • 0

Yes, I believe everybody here is aware of that.

I know most of us are aware of it but people starting with programming like OP might not know that ;)

Link to comment
Share on other sites

  • 0

 

As of html 5 a script declaration is unnecessary. For some reason, the op is using xhtml which should use it for standards but should still work. 

 

Whats wrong here:

Case sensitivity: you name your function logon, and then call the function Logon that doesnt exist.

Function outside script tags. Why are you just calling "logon" in the body? You need to wrap all javascript in script tags (apart from some cases like forms).

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<script>
function logon() {
	var userid;
 	var password;
 	var valid
 	
 	userid=prompt('enter userid','');
 	password=prompt('enter password','');
 	valid=validatelogon(userid,password);
 
 	if(valid==true) {
 		alert('valid logon');
 	} else {
 		alert('invalid logon');
 	}
 
function validatelogon(id,pwd) {
 	var Returnvalue;
 	if(id=='peter' && pwd =='tree') {
 		Returnvalue=true;
 	} else {
 		Returnvalue=false;
 	}
 return Returnvalue;
}

}
</script>
</head>
 
<body>
<script>
logon();
</script>
 
</body>
</html>

 

I copied and paste it and it works fine I am gonna compare text  I am using dreamweaver because the colors tell you went you spell somethign wrong or forget a tag if document.write is not pink you know its spelled wrong is their a program thats shows missing semi colons and so on

 

Link to comment
Share on other sites

  • 0

I copied and paste it and it works fine I am gonna compare text  I am using dreamweaver because the colors tell you went you spell somethign wrong or forget a tag if document.write is not pink you know its spelled wrong is their a program thats shows missing semi colons and so on

I don't think so, many editors often automatically insert them for you tho. Technically JavaScript doesnt need you to write the semicolons because of Automatic semicolon insertion, however, I advise you should learn where to use them. http://inimino.org/~inimino/blog/javascript_semicolons 

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.