You are on page 1of 17

Assignment 1

Name : Bhavna Narwade


Div : B
Roll No : 76
Subject : Javascript
Q 1) Write JavaScript Program To Print Hello World.
Program:
<!DOCTYPE HTML>
<html>
<body>
<script>
console.log('Hello World');
</script>
</body>
</html>

Output:

Q 2) Write JavaScript Program to Find the Square Root


Program:
<!DOCTYPE html>
<html>
<body>
<script>
const number = prompt('Enter the number: ');
const result = Math.sqrt(number);
console.log(`The square root of ${number} is ${result}`);
</script>
</body>
</html>

Output:

Q 3) Write JavaScript Program to Calculate the Area of


a Triangle.
Program:
<!DOCTYPE html>

<html>

<body>

<script>

var base = parseInt(prompt("Enter the base: "));

var height = parseInt(prompt("Enter the height: "));


var area = (base * height) / 2;

console.log("Base: " + base);

console.log("Height: " + height);

console.log("The area of the triangle is " + area);

</script>

</body>

</html>

Output:
Q 4) Write JavaScript Program to Check if a number is
Positive, Negative, or Zero.
Program:
<!DOCTYPE html>

<html>

<body>

<script>

const number = parseInt(prompt("Enter a number: "));

if (number > 0)

console.log("The number is positive");

else if (number == 0)

console.log("The number is zero");

else

console.log("The number is negative");

</script>

</body>
</html>

Output:

Q 5) Write JavaScript Program to Check Prime


Number
Program:
<!DOCTYPE html>

<html>

<body>

<script>

var num = 11;

var i = 1;

var count = 0;

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


{

if(num % i == 0)

count++;

if(count == 2)

document.write(num +" is a prime number");

else

document.write(num +" is not a prime number");

</script>

</body>

</html>

Output:

Q 6) Create a simple multiplication table asking the


user the number of rows and columns he wants
Program:
<html>

<head>

<title>Multiplication Table</title>

</head>

<body>
<script>

var rows = prompt("Insert rows for your multiplication table.");

var cols = prompt("Insert columns for your multiplication table.");

if(rows == "" || rows == null)

rows = 10;

if(cols== "" || cols== null)

cols = 10;

createTable(rows, cols);

function createTable(rows, cols)

var j=1;

var output = "<table border='1' width='500'


cellspacing='0'cellpadding='5'>";

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

output = output + "<tr>";

while(j<=cols)

output = output + "<td>" + i*j + "</td>";

j = j+1;

output = output + "</tr>";

j = 1;

}
output = output + "</table>";

document.write(output);

</script>

</body>

</html>

Output:
Q 7) Write a JavaScript function that reverse a number.
Program:

<!doctype html>

<html>

<head>

<title>

Reverse Number

</title>

<script>

function palin()

var a,n,b,temp=0;
n=Number(document.getElementById("n_input").value);

b=n;

while(n>0)

a=n%10;

n=parseInt(n/10);

temp=temp*10+a;

alert(temp);

</script>

</head>

<body>

Enter any Number: <input id="n_input">

<button onclick="palin()">

Check

</button>

</body>

</html>

Output:
Q 8) Write a JavaScript function that checks whether a
passed string is palindrome or not?
Program:

<!DOCTYPE html>

<html>

<body>

<script>

function check_palindrome( str )

let j = str.length -1;

for( let i = 0 ; i < j/2 ;i++)

let x = str[i] ;

let y = str[j-i];

if( x != y)

return false;

}
return true;

function is_palindrome( str )

let ans = check_palindrome(str);

if( ans == true )

console.log("passed string is palindrome ");

else

console.log("passed string not a palindrome");

let test = "dad";

is_palindrome(test);

</script>

</body>

</html>

Output:
Q 9) Write a JavaScript function which will take an
array of numbers stored and find the second lowest and
second greatest numbers, respectively
Program:
<!DOCTYPE html>

<html>

<head>

<title>

Find the second lowest and second greatest numbers from an array

</title>

</head>

<body>

<script>

function Second_Greatest_Lowest(arr_num)

arr_num.sort(function(x,y)

return x-y;

});

var uniqa = [arr_num[0]];

var result = [];


for(var j=1; j < arr_num.length; j++)

if(arr_num[j-1] !== arr_num[j])

uniqa.push(arr_num[j]);

result.push(uniqa[1],uniqa[uniqa.length-2]);

return result.join(',');

console.log(Second_Greatest_Lowest([1,2,3,4]));

</script>

</body>

</html>

Output:

Q 10) Write a JavaScript program to sort the items of


an array.
Sample array : var arr1 = [ 3, 8, 7, 6, 5, -4, -3, 2, 1 ];
Sample Output : -4,-3,1,2,3,5,6,7,8
Program:
<!DOCTYPE html>

<html>

<head>

<title>

sort the items of an array

</title>

</head>

<body>

<script>

var arr1=[3, 8, 7, 6, 5, -4, -3, 2, 1 ];

var arr2=[];

var min=arr1[0];

var pos;

var max=arr1[0];

for (i=0; i<arr1.length; i++)

if (max<arr1[i]) max=arr1[i];

for (var i=0;i<arr1.length;i++)

{
for (var j=0;j<arr1.length;j++)

if (arr1[j]!="x")

if (min>arr1[j])

min=arr1[j];

pos=j;

arr2[i]=min;

arr1[pos]="x";

min=max;

console.log(arr2);

</script>

</body>

</html>

Output:

You might also like