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

JS Code

The document contains 6 JavaScript functions with examples: 1) A function to reverse a number. 2) A function to capitalize the first letter of a string. 3) A function to count vowels in a string. 4) A function to find the second lowest and second greatest numbers in an array. 5) A recursive function to check if a number is prime. 6) A function to get all possible subsets of a fixed length from an array.

Uploaded by

Rahul Raj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JS Code

The document contains 6 JavaScript functions with examples: 1) A function to reverse a number. 2) A function to capitalize the first letter of a string. 3) A function to count vowels in a string. 4) A function to find the second lowest and second greatest numbers in an array. 5) A recursive function to check if a number is prime. 6) A function to get all possible subsets of a fixed length from an array.

Uploaded by

Rahul Raj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1)Write a Javascrpt function that reverse a number.

function reverseFunction(num) {
let numStr = num.toString();
let reversedStr = '';
for (let i = numStr.length - 1; i >= 0; i--) {
reversedStr += numStr[i];
}
return parseInt(reversedStr);
}

let num = 785632893


;
let reversedNum = reverseFunction(num);

console.log(reversedNum);

2)Write a Javascipt function that accept a string as a parameter and convert the
first
letter into upper case.
function capitalizeFirstLetter(str) {
const capitalized = str.charAt(0).toUpperCase() + str.slice(1);
return capitalized;
}
const string = prompt('Enter a string: ');
const result = capitalizeFirstLetter(string);
console.log(result);

3)Write a Javascipt function that accept a string as a parameter and count the no.
of
vowel in string.
function countvowels(str){
let count =0;
for (const char of str){
if (
char==="a"||
char==="e"||
char==="i"||
char==="o"||
char==="u"
){
count++;
}
}
console.log(count);
}

4)Write a Javascipt function that takes a aaray of no. and find the second lowest
no.
and second greatest no. respectively.
function findSecondLowestAndGreatest(numbers) {
// Sort the array in ascending order
numbers.sort(function(a, b) {
return a - b;
});

// Second lowest number is at index 1


var secondLowest = numbers[1];

// Second greatest number is at the second last index


var secondGreatest = numbers[numbers.length - 2];

return {
secondLowest: secondLowest,
secondGreatest: secondGreatest
};
}

5)Write a Javascipt function that accept no. as parameter and check whether is
prime or
not using recursion.
// Example usage:
var numbers = [100, 3, 11, 86, 56];
var result = findSecondLowestAndGreatest(numbers);
console.log("Second Lowest Number:", result.secondLowest);
console.log("Second Greatest Number:", result.secondGreatest);
function isPrime(number, divisor = 2) {
// Base cases
if (number <= 2) {
return (number === 2) ? true : false;
}
if (number % divisor === 0) {
return (divisor === number) ? true : false;
}

// Recursive case
return isPrime(number, divisor + 1);
}

// Example usage:
console.log(isPrime(7)); // Output: true
console.log(isPrime(12)); // Output: false

6)Write a Javascipt function to get all possible subsets with a fixed length
combinations in array.
function combinations(array, length) {
var result = [];

// Recursive function to generate combinations


function generateCombinations(current, start) {
if (current.length === length) {
result.push(current.slice()); // Add the combination to the result
return;
}
for (var i = start; i < array.length; i++) {
current.push(array[i]);
generateCombinations(current, i + 1);
current.pop(); // Backtrack
}
}

generateCombinations([], 0);
return result;
}

// Example usage:
var array = [1, 2, 3, 4];
var length = 2;
var result = combinations(array, length);
console.log(result);

You might also like