Jump to content

Recommended Posts

Posted

Javascript tutorial 2

I feel very bad today and i don't know how many errors i will have or how bad i will expain it but i hope you will undarstand it if you have problem ask in comments

I hope that you have undarstand part 1 in this part we will cover Functions, If...Else, Switch

 

Function

 

A function is a block of code that will be executed when somebody call it, in JavaScript functions are very used

 

Exaple

 

function MyFunction()
{
alert("My first function");
}

 

 

On the first line we give a name to the function (MyFunction in the example)

between {} is the body of the function (the code we what to use later[when we call the function]))

And now let's call it

 

<body>
<button onlick="MyFunction()">Function</button>
</body>

 

 

This code is simple to undarstand i hope you deal with it

 

Calling function with arguments

When you call a function you can pass along some values to it these values are called arguments or parameters

example

 

 

<body>

<button onclick="myFunction('Max','Cheaters')">arguments</button>

<script>
function myFunction(name,name1)
{
alert("Welcome "   name   " to "   name1);
}
</script>

</body>

 

Here we give values to name and name1

 

<button onclick="myFunction('Max','Cheaters')">arguments</button>

 

 

and the function work with those 2 arguments you can have 5-10 even 1235135351325 arguments on function

 

If...Else

 

if statement - use this statement to execute some code only if a specified condition is true

if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false

if...else if....else statement - use this statement to select one of many blocks of code to be executed

switch statement - use this statement to select one of many blocks of code to be execute

 

If statment example

 

 

if (time<18, time>13)
{
    alert(Have a nice day);
}

 

 

if the times is between 13 and 18 the program will alert (have a nice day)

 

If...else Statement

 

if (time<20)
  {
  alert("Good day");
  }
else
  {
  alert("Good evening");
  }

 

If its < than 20 it will alert good day if its > than 20 it will alert good evening

 

If...else if...else Statement

 

 

 

if (time<10)
{
alert("Good morning");
}
else if (time<20)
{
alert("Good day");
}11
else
{
alert("Good evening");

 

 

If the time is less than 10:00, you will get a Good morning alert if not but the time is less than 20:00 you will get a Good day alert otherwise you will get a Good evening alert

 

Switch

We use the switch statement to select one of many blocks of code to be executed

 

Example

 

var day=new Date().getDay();
switch (day)
{
case 0:
x="Today it's Sunday";
break;
case 1:
x="Today it's Monday";
break;
case 2:
x="Today it's Tuesday";
break;
case 3:
x="Today it's Wednesday";
break;
case 4:
x="Today it's Thursday";
break;
case 5:
x="Today it's Friday";
break;
case 6:
x="Today it's Saturday";
break;
}

 

 

Display today's weekday-name. Note that Sunday=0, Monday=1, Tuesday=2, etc

 

Don't forget to write break; afther each case !

 

 

`Thanks 8)

Posted

Just a little advice, for ppl to find arround your topics easier..

 

hmm Okay i will make it Tommorow i have to make some shares right now ;D

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock