JavaScript Program to Find Factorial of a
Number
// JavaScript Program to Find Factorial of a Number
var num = parseInt(prompt("Enter a positive integer: "));
// Checking if number is negative
if (num < 0){
console.log("Factorial of negative numbers doesn't exist.");
}
// Checking whether number equals to zero
else if (num == 0){
console.log("Factorial of 0 is: 1");
}
// Checking if number is positive
else{
let fact = 1;
for (i = 1; i <= num; ++i){
fact = fact * i;
}
console.log(`Factorial of ${num} is: ${fact}.`);
}
JavaScript Program To Find Average of 3
Numbers
//JavaScript Program To Find Average of 3 Numbers
//Take User Input
var a = parseInt(prompt("Enter First Number: "));
var b = parseInt(prompt("Enter Second Number: "));
var c = parseInt(prompt("Enter Third Number: "));
//Calculate Average
var average = (a + b + c) / 3;
//Display Output
console.log("The average of three numbers: " + average);
JavaScript Program To Find Average of N
Numbers
var start = parseInt(prompt("Enter the Starting Number"));
var end = parseInt(prompt("Enter the Ending Number"));
var total = 0;
var count = (end - start) + 1
var average = 0;
console.log("Average of Numbers between " + start + " and " + end + " is: " );
for (start = start; start <= end; start++)
total = total + start;
average = total / count;
console.log("Total Sum: " + total);
console.log("No. of elements: " + count);
console.log("Average: " + average);
Output
Average of Numbers between 1 and 5 is:
Total Sum: 15
No. of elements: 5
Avera
JavaScript Program To Calculate The
Area of a Triangle
//JavaScript Program To Calculate The Area of a Triangle
var base = parseInt(prompt("Enter the base: "));
var height = parseInt(prompt("Enter the height: "));
//Calculating the area
var area = (base * height) / 2;
//Display Output
console.log("Base: " + base);
console.log("Height: " + height);
console.log("The area of the triangle is " + area);
Output
Base: 3
Height: 4
The area of the triangle is 6
JavaScript Program To Find The Square
Root
var num = prompt("Enter a Number");
var root = Math.sqrt(num);
// displays square root
console.log("The Square Root of " + num + " is " + root);
JavaScript Program To Add Two Numbers
// JavaScript Program To Add Two Numbers
// Define variable with integers
var num1 = 2;
var num2 = 3;
// add two numbers
var sum = num1 + num2;
// display output
console.log("The Sum of " + num1 + " and " + num2 + " is: " + sum);
Output
The Sum of 2 and 3 is: 5
JavaScript Program To Add Two Numbers
with User Input
// Take user input
var num1 = parseInt(prompt("Enter first number: "));
var num2 = parseInt(prompt("Enter second number: "));
// Add two numbers
var sum = num1 + num2;
// Display output
console.log("The sum of " + num1 + " and " + num2 + " is: " + sum);
Output
The sum of 5 and 7 is: 12
Example 1: Check Palindrome Using for Loop
// program to check if the string is palindrome or not
function checkPalindrome(string) {
// find the length of a string
const len = string.length;
// loop through half of the string
for (let i = 0; i < len / 2; i++) {
// check if first and last string are same
if (string[i] !== string[len - 1 - i]) {
return 'It is not a palindrome';
}
}
return 'It is a palindrome';
}
// take input
const string = prompt('Enter a string: ');
// call the function
const value = checkPalindrome(string);
console.log(value);
Run Code
Output
Enter a string: madam
It is a palindrome
In the above program, the checkPalindrome() function takes input from the
user.
The length of the string is calculated using the length property.
The for loop is used to iterate up to the half of the string. The if condition is
used to check if the first and the corresponding last characters are the
same. This loop continues till the half of the string.
During the iteration, if any character of the string, when compared with its
corresponding last string is not equal, the string is not considered a
palindrome.
Example 2: Check Palindrome using built-in
Functions
// 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);
Run Code
Output
Enter a string: hello
It is not a palindrome
In the above program, the palindrome is checked using the built-in methods
available in JavaScript.
The split('') method converts the string into individual array characters.
const arrayValues = string.split(''); // ["h", "e", "l", "l", "o"]
The reverse() method reverses the position in an array.
// ["o", "l", "l", "e", "h"]
const reverseArrayValues = arrayValues.reverse();
The join('') method joins all the elements of an array into a string.
const reverseString = reverseArrayValues.join(''); // "olleh"
Then the if...else statement is used to check if the string and the reversed
string are equal. If they are equal, the string is a palindrome.
Note: The multiple lines of code can be reduced and written in one line:
const reverseString = string.split('').reverse().join('');