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)

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...