Reverse a String
Explanation: This program reverses a given string.
Code:
function reverseString(str) {
return str.split('').reverse().join('');
}
const str = prompt("Enter a string:");
console.log("Reversed string: " + reverseString(str));
output:
Enter a string: hello
Reversed string: olleh
Check Prime Number
Explanation: This program checks if a given number is prime.
Code:
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
const num = parseInt(prompt("Enter a number:"), 10);
if (isPrime(num)) {
console.log(num + " is a prime number.");
} else {
console.log(num + " is not a prime number.");
}
output:
Enter a number: 29
29 is a prime number.
Sum of Array Elements
Explanation: This program calculates the sum of elements in an array.
Code:
function sumArray(arr) {
return arr.reduce((a, b) => a + b, 0);
}
const n = parseInt(prompt("Enter the number of elements:"), 10);
let arr = [];
for (let i = 0; i < n; i++) {
arr.push(parseInt(prompt(`Enter element ${i + 1}:`), 10));
}
console.log("The sum of the array elements is: " + sumArray(arr));
output:
Enter the number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
The sum of the array elements is: 15
Factorial of a Number
Explanation: This program calculates the factorial of a given number.
Code:
function factorial(num) {
if (num === 0 || num === 1) return 1;
return num * factorial(num - 1);
}
const num = parseInt(prompt("Enter a number:"), 10);
console.log("The factorial of " + num + " is: " + factorial(num));
output:
Enter a number: 5
The factorial of 5 is: 120
Check Palindrome Number
Explanation: This program checks if a given number is a palindrome.
Code:
function isPalindrome(num) {
const original = num.toString();
const reversed = original.split('').reverse().join('');
return original === reversed;
}
const num = parseInt(prompt("Enter a number:"), 10);
if (isPalindrome(num)) {
console.log(num + " is a palindrome number.");
} else {
console.log(num + " is not a palindrome number.");
}
output:
Enter a number: 121
121 is a palindrome number.
Fibonacci Series
Explanation: This program prints the Fibonacci series up to a given number of terms.
Code:
function fibonacci(n) {
let t1 = 0, t2 = 1, nextTerm;
let series = [];
for (let i = 1; i <= n; ++i) {
series.push(t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return series.join(', ');
}
const n = parseInt(prompt("Enter the number of terms:"), 10);
console.log("Fibonacci Series: " + fibonacci(n));
output:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Binary Search
Explanation: This program implements binary search on a sorted array.
Code:
function binarySearch(arr, key) {
let low = 0, high = arr.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (arr[mid] === key) return mid;
if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}
const n = parseInt(prompt("Enter the number of elements:"), 10);
let arr = [];
for (let i = 0; i < n; i++) {
arr.push(parseInt(prompt(`Enter element ${i + 1}:`), 10));
}
arr.sort((a, b) => a - b);
const key = parseInt(prompt("Enter the key to search:"), 10);
const result = binarySearch(arr, key);
if (result !== -1) {
console.log("Element found at index: " + result);
} else {
console.log("Element not found.");
}
output:
Enter the number of elements: 5
Enter element 1: 1
Enter element 2: 3
Enter element 3: 5
Enter element 4: 7
Enter element 5: 9
Enter the key to search: 7
Element found at index: 3
Insertion Sort
Explanation: This program implements insertion sort on an array.
Code:
function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
const n = parseInt(prompt("Enter the number of elements:"), 10);
let arr = [];
for (let i = 0; i < n; i++) {
arr.push(parseInt(prompt(`Enter element ${i + 1}:`), 10));
}
insertionSort(arr);
console.log("Sorted array: " + arr.join(' '));
output:
Enter the number of elements: 5
Enter element 1: 5
Enter element 2: 2
Enter element 3: 9
Enter element 4: 1
Enter element 5: 5
Sorted array: 1 2 5 5 9
Check Palindrome String
Explanation: This program checks if a given string is a palindrome.
Code:
function isPalindrome(str) {
const reversed = str.split('').reverse().join('');
return str === reversed;
}
const str = prompt("Enter a string:");
if (isPalindrome(str)) {
console.log(str + " is a palindrome string.");
} else {
console.log(str + " is not a palindrome string.");
}
output:
Enter a string: madam
madam is a palindrome string.
0 Comments:
Post a Comment