0% found this document useful (0 votes)
5 views4 pages

Javascript Program

The document contains JavaScript programs demonstrating various functionalities, including swapping two variables without a temporary variable, checking if a year is a leap year, determining if a number is prime, listing prime numbers within a range, and calculating the factorial of a number. Each section includes example usage to illustrate the functionality. The code snippets are concise and serve as practical examples for common programming tasks.

Uploaded by

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

Javascript Program

The document contains JavaScript programs demonstrating various functionalities, including swapping two variables without a temporary variable, checking if a year is a leap year, determining if a number is prime, listing prime numbers within a range, and calculating the factorial of a number. Each section includes example usage to illustrate the functionality. The code snippets are concise and serve as practical examples for common programming tasks.

Uploaded by

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

Javascript Program

program to swap two variables without temp variable.

let a = 5;

let b = 10;

// Swapping without a temporary variable using destructuring

[a, b] = [b, a];

console.log("After swapping:");

console.log("a =", a);

console.log("b =", b);

Leap Year

function isLeapYear(year) {

// Check if the year is divisible by 4

if (year % 4 === 0) {

// Check if it's divisible by 100

if (year % 100 === 0) {

// If divisible by 100, it must also be divisible by 400

if (year % 400 === 0) {

return true; // It's a leap year

} else {

return false; // Not a leap year

} else {

return true; // It's a leap year

} else {

return false; // Not a leap year

}
// Example usage:

const year = 2024; // You can change this value

if (isLeapYear(year)) {

console.log(`${year} is a Leap Year.`);

} else {

console.log(`${year} is not a Leap Year.`);

Prime number:

const checkPrime = (num) => {

if (num <= 1) {

return false; // Numbers less than or equal to 1 are not prime

if (num === 2) {

return true; // 2 is the only even prime number

// Check divisibility from 2 to the square root of the number

for (let i = 2; i <= Math.sqrt(num); i++) {

if (num % i === 0) {

return false; // Number is divisible by i, so it's not prime

return true; // Number is prime if no divisors found

};

// Example usage

const num = 29; // You can change this to any number

if (checkPrime(num)) {

console.log(`${num} is a Prime Number.`);


} else {

console.log(`${num} is not a Prime Number.`);

Prime number list:

const lower = 1;

const upper = 10;

console.log(`Prime numbers between ${lower} and ${upper} are:`);

for (let num = lower; num <= upper; num++) {

if (num > 1) { // All prime numbers are greater than 1

let isPrime = true;

for (let i = 2; i < num; i++) { // Check divisibility from 2 to num-1

if (num % i === 0) {

isPrime = false;

break;

if (isPrime) {

console.log(num); // If no divisors were found, num is prime

Find the Factorial of a Number

const factorial = (num) => {

let result = 1;
for (let i = 1; i <= num; i++) {

result *= i; // Multiply result by i at each iteration

return result;

};

const num = 5; // You can change this to any number

console.log(`${num}! = ${factorial(num)}`);

You might also like