0% found this document useful (0 votes)
5 views

All JS Output-1

The document contains various JavaScript programs that demonstrate basic programming concepts such as generating multiplication tables, calculating areas of geometric shapes (triangle, rectangle, circle), string manipulation (reversing, replacing characters, checking for palindromes), and string comparison using different methods. Each program is presented with HTML structure and includes comments explaining the functionality. The document serves as a practical guide for learning JavaScript programming.

Uploaded by

hafgfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

All JS Output-1

The document contains various JavaScript programs that demonstrate basic programming concepts such as generating multiplication tables, calculating areas of geometric shapes (triangle, rectangle, circle), string manipulation (reversing, replacing characters, checking for palindromes), and string comparison using different methods. Each program is presented with HTML structure and includes comments explaining the functionality. The document serves as a practical guide for learning JavaScript programming.

Uploaded by

hafgfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical No.

01
Roll No. 45

Program:-
<html>
<head>
<title> To generate the multiplication table of a given number. </title>
</head>
<body>
<script>
var table = 10;
for(var i=1;i<=10;i++)
{document.write(table + "x"+ i + "="+ i*table +"<br>"); // 10* 1= 10
}
</script>
</body>
</html>
Practical No. 02
Roll No. 45

Program: To calculate area of triangle


<html>
<head>
<title> JavaScript Program To Calculate The Area of a Triangle </title>
</head>
<body>
<script>
// enter the value of base and height of the triangle
const baseValue = prompt('Enter the base of a triangle: ');
const heightValue = prompt('Enter the height of a triangle: ');
// calculate the area
const areaValue = (baseValue * heightValue) / 2;
//Display Output
console.log(`The area of the triangle is ${areaValue}`);
</script>
</body>
</html>

Output:
Program:- To calculate area of Rectangle
<html>
<head>
<title> JavaScript Program To Calculate The Area of a Rectangle </title>
</head>
<body>
<script type="text/javascript">
/*
Javascript program to find the area of a rectangle
*/
var l, w, a;
l = 8;
w = 6;
/* Calculate area of rectangle */
a = l * w;
document.write("Area of rectangle = " + a + " units");
</script>
</body>
</html>

Output:
Program:- To calculate the area of circle
<html>
<head>
<title> JavaScript Program To Calculate The Area of a Circle </title>
</head>
<body>
<h1>Area of a circle</h1>
Enter the radius for your circle:
<input type="text" id="txtRadius" />
<input type="button" value="Calculate" onClick=CalculateArea() />
<script>
function CalculateArea() {
var radius = document.getElementById('txtRadius').value;
alert("The area of the circle is " + (radius * radius * Math.PI));
}
</script>
</body>
</html>

Output:
Practical No. 03
Roll No. 45

Program :-
<html>
<head>
<title> program to reverse a string </title>
</head>
<body>
<script type="text/javascript">
function reverseString(str) {
// return a new array of strings
const arrayStrings = str.split("");
// reverse the new created array elements
const reverseArray = arrayStrings.reverse();
// join all elements of the array into a string
const joinArray = reverseArray.join("");
// return the reversed string
return joinArray;
}
// take input from the user
const string = prompt('Enter a string: ');
const result = reverseString(string);
console.log(result);
</script>
</body>
</html>

Output:
Program: Replace characters of a string
<html>
<head>
<title> program to reverse a string </title>
</head>
<body>
<script type="text/javascript">
// program to replace a character of a string
const string = 'Hello!!! I am Mohit Wani';
// regex expression
const regex = /red/g;
// replace the characters
const newText = string.replace(regex, 'blue');
// display the result
console.log(newText);
</script>
</body>
</html>
Output:
Program: String is Palindrome
</head>
<body>
<script type="text/javascript">
// program to check if the string is palindrome or not
function checkPalindrome(string) {
// convert string to an array
const arrayValues = string.split('');
// reverse the array values
const reverseArrayValues = arrayValues.reverse();
// convert array to string
const reverseString = reverseArrayValues.join('');
if(string == reverseString) {
console.log('It is a palindrome');
}
else {
console.log('It is not a palindrome');
}
}
//take input
const string = prompt('Enter a string: ');
checkPalindrome(string);
</script>
</body>
</html>

Output:
Practical No. 04
Roll No. 45

Program :- Using equality operators


<html>
<head>
<title> program to perform string comparison </title>
</head>
<body>
<script type="text/javascript">
var stringFirst = "javascript world";
var stringSecond = "javascript world";
var res = '';
if(stringFirst == stringSecond)
{
res = 'strings are equal';
}else
{
res = 'strings are not equal';
}
document.write( "Output :- " + res );
</script>
</body>
</html>

Output:
Program: Using toUpperCase()
<html>
<head>
<title> program to perform string comparison </title>
</head>
<body>
<script type="text/javascript">
const string1 = 'Are you like JavaScript Program';
const string2 = 'javascript program';
// compare both strings
const result = string1.toUpperCase() === string2.toUpperCase();
if(result) {
console.log('The strings are similar.');
} else {
console.log('The strings are not similar.');
}
</script>
</body>
</html>

Output:
Program:JS String Comparison Using RegEx
<html>
<head>
<title> program to perform string comparison </title>
</head>
<body>
<script type="text/javascript">
const string1 = 'JavaScript Program';
const string2 = 'javascript program';
// create regex
const pattern = new RegExp(string1, "gi");
// compare the stings
const result = pattern.test(string2)
if(result) {
console.log('The strings are similar.');
} else {
console.log('The strings are not similar.');
}
</script>
</body>
</html>

Output:
Program: localeCompare() Method
<html>
<head>
<title> program to perform case insensitive string comparison </title>
</head>
<body>
<script type="text/javascript">
const string1 = 'This is the JavaScript Program';
const string2 = 'this is the javascript program';
const result = string1.localeCompare(string2, undefined, { sensitivity: 'base' });
if(result == 0) {
console.log('The strings are similar.');
} else {
console.log('The strings are not similar.');
}
</script>
</body>
</html>

Output:

You might also like