js3

Share this page :

Example of Odd-Even:

<html>
<body>
<script>

var a=prompt(“Enter a number:”)

document.write(“<h1>Odd-Even Check</h1>”)

document.write(“<hr>”)

document.write(“You enter this number :”+a)

if(a%2==0)

{
document.write(“<br>The number is Even”);
}

else

{
document.write(“<br>The number is Odd”);
}

</script>
</body>
</html>

Calculate the mark sheet:

<html>
<body>
<script>

var a=prompt(“Enter 1st number:”)

var b=prompt(“Enter 2nd number:”)

var c=prompt(“Enter 3rd number:”)

var d=prompt(“Enter 4th number:”)

var sum,avg,x

sum=parseInt(a) + parseInt(b) + parseInt(c) + parseInt(d)

avg=parseFloat(sum/4)

if(avg>=80)

{
x=”Grade A+”
}

else if(avg>=70)
{
x=”Grade A”
}

else if(avg>=60)
{
x=”Grade B”
}

else if(avg>=50)
{
x=”Grade C”
}

else
{
x=”FAIL”
}

document.write(“1st number:”+a+”<br>”)

document.write(“2nd number:”+b+”<br>”)

document.write(“3rd number:”+c+”<br>”)

document.write(“4th number:”+d+”<br>”)

document.write(“<hr>”)

document.write(“<br>Total:”+sum+”<br>”)

document.write(“Average:”+avg+”<br>”)

document.write(“Grade:”+x)

</script>
</body>
</html>

Loop:
Looping means repeating a block of code when a condition is true.

for loop:
The for statement enables you to repeat a block of code a certain number of times.

Syntax:
for (Initialize loop variable; Loop test condition; Increment loop variable)
{
Code looped through
}

Example 1:

<html>
<script>
document.write(“<h1>for loop increment</h1>”)
var x
for(x=1; x<=10; x++)
document.write(“<br>” + x)
</script>
</html>

Example 2:

<html>
<script>
document.write(“<h1>for loop decrement</h1>”)
var x
for(x=10; x>=1; x–)
document.write(“<br>” + x)
</script>
</html>

Example 3:

<html>

<script>

var i,sum=0;

for(i=1;i<=5;i++)

{
sum=sum+i;
}

document.write(“<h1>Addition of 1 to 5</h1>”)

document.write(“Result:”+sum)

</script>

</body>
</html>

while loop:

JavaScript includes while loop to execute code repeatedly till it satisfies a specified condition. Unlike for loop, while loop only requires condition expression.

Syntax:
while(condition expression)
{
/* code to be executed
till the specified condition is true */
}

Example:

<html>
<script>
var i =5;

while(i < 12)
{
document.write(i+”<br>”);
i++;
}
</script>
</html>

What is string?
A string is just a series of characters & each character has a position, or index. The first character is in position 0, the second in 1, and so on.

Character Index: 0 1 2 3 4
Character:             H e l l o

The length Property:

The length property simply returns the number of characters in the string.

<html>
<body>
<h1>String Length</h1>
<hr color=red>

<script>

var a = “welcome”

document.write(“The string is = ” + a)

document.write(“<br>Length = ” + a.length)

</script>
</body>
</html>

Javascript method:

Methods are the actions that can be performed on objects.

1. fromCharCode() method converts values to characters.

2. The charAt() method returns that character.

3. The charCodeAt () method is similar in use to the charAt() method, but instead of returning the character itself, it returns a number that represents the decimal character code.

4. toUpperCase() method of the string object to display a text in capital letters.

5. toLowerCase() method of the string object to display a text in small letters.

Example 1:

<html>
<body>
<script>

var s = ” ”

var c

for (c = 65; c <= 90; c++)
{
s = s + String.fromCharCode(c) + “<br>”
}

document.write(s)
</script>
</body>
</html>

Example 2:

<html>
<body>

<h1>The charAt() Method</h1>

<h2>The string is WELCOME</h2>

<script>

var s = “WELCOME”

var f = s.charAt(5)

document.write(“<h2>The fifth character is ” + f + “</h2>”)

</script>
</body>
</html>

Example 3:

<html>
<body>
<h1>The charCodeAt () Method </h1>

<script>
var s = “WELCOME”

var c = s.charCodeAt(5)

document.write(“The string is WELCOME”)

document.write(“<br>The fifth character code is ” + c)

</script>
</body>
</html>

Example 4 & 5:

<html>
<body>
<script>
var s=prompt(“enter your name in small letter”)

document.write(“Upper Case String = ” + s.toUpperCase())

var ss=prompt(“enter your name in capital letter”)

document.write(“<br>Lower Case String = ” + s.toLowerCase() )

</script>
</body>
</html>

Array

In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index. An array is similar to a normal variable. A normal variable can only hold one piece of data at a time.

The difference between such a normal variable and an array is that an array can hold more than one item of data at the same time.

Characteristics:

  1. An array can hold values of different types.
  2. The length of an array is dynamically sized and auto-growing.

<html>

<body>
<script>

var a = new Array()

a[0] = “Ram”
a[1] = “Shyam”
a[2] = “Jadu”

document.write(“<h1>Example of Array</h1>”)

document.write(“a[0] = ” + a[0] + “<BR>”)

document.write(“a[2] = ” + a[2] + “<BR>”)

document.write(“a[1] = ” +a[1] + “<BR>”)

a[1] = “Laxman”

document.write(“<hr color=red>”)

document.write(“a[1] changed to ” + a[1])

</script>
</body>
</html>

Example 2:

<html>
<body>
<script>
var c,d
var a= new Array()

var b=prompt(“How many names do you want to enter”)

for(c=1;c<=b;c++)

{

d=prompt(“enter names”)

a=d[c]

document.write(d + “<br>”)

}
</script>
</body>
</html>

Events:
Every element on a web page has certain events which can trigger a JavaScript. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the html tags.

onLoad and onUnload:

the Onload and onUnload events are triggered when the user enters or leaves the page.

<html>
<body onLoad=”alert(‘good morning’)” onUnload=”alert(‘good night’)”>
</body>
</html>

onMouseover and onMouseOut event:
onMOuseOver and onMouseOut are often to create animate button.

<html>
<script>

function x()
{
alert(“MouseOver & MouseOut Event”)
}
</script>

<body >
<h1><center>MouseOver & MouseOut Event</center></h1>

<hr color=”yellow”>

<h1><center>Move the mouse pointer on the button</center></h1>

<hr color=”blue”>

<center>

<input type=”button” value=”click” onMouseOver=’document.bgColor=”red” ‘
onMouseOut=’document.bgColor=”white” ‘ onclick=”x()”>

</center>
</body>
</html>

Example 2:

<html>
<head>
<script>
var c=0
</script>
</head>

<body onClick=”++c; alert(‘You have clicked ‘ +c+ ‘ times’)” >
<h1><u>click count events </u></h1>
<h2>click on the body</h2>

</body>
</html>

The Math Object:
The Math object provides a number of useful mathematical functions and number manipulation methods.

The abs() Method:
It returns the absolute value of the number. Essentially, this means that it returns the positive value of the number.

<html>
<body>
<script>

var n = -101;

document.write(“The number is ” +n)

document.write(“The absolute value of the number is ” + Math.abs(n) )

</script>
</body>
</html>

The pow() Method:
The pow() method raises a number to a specified power.

<html>
<body>
<script>

var num1 = prompt(“Enter the number “)

var num2 = prompt(“Enter the power “)

document.write(“<h2> The number is ” + num1 + “</h2>”)

document.write(“<h2> The power is ” + num2 + “</h2>”)

document.write(“<h2>Result: ” + Math.pow(num1,num2) + “</h2>”)

</script>
</body>
</html>

The sqrt() Method:
The sqrt() method returns the square root of a number.

<html>
<body>
<script>

var num1 = prompt(“Enter the number “)

document.write(“<h1><center>Square Root</center></h1>”)

document.write(“<h2> The number is: ” + num1 + “</h2>”)

document.write(“Square root of the number is: ” + Math.sqrt(num1))

</script>
</body>
</html>

Share this page :