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

JAVASCRIP Prog

Java script

Uploaded by

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

JAVASCRIP Prog

Java script

Uploaded by

Bhargavi Japala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Check for Anagrams

function isAnagram(str1, str2) {

const sortedStr1 = str1.split('').sort().join('');

const sortedStr2 = str2.split('').sort().join('');

return sortedStr1 === sortedStr2;

const inputStr1 = "listen";

const inputStr2 = "silent";

const isAnagramResult = isAnagram(inputStr1, inputStr2);

console.log(isAnagramResult); // Output: true

Check for Palindrome

function isPalindrome(str) {

const reversedStr = str.split('').reverse().join('');

return str === reversedStr;

const inputString = "level";

const isPalindromeResult = isPalindrome(inputString);

console.log(isPalindromeResult); // Output: true

Check for Prime Number


function isPrime(number) {
if (number < 2) {
return false;
}
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false;
}
}
return true;
}
const inputNumber = 7;
const isPrimeResult = isPrime(inputNumber);
console.log(isPrimeResult); // Output: true

Count the Occurrences of a Character in a String

function countOccurrences(str, char) {


let count = 0;
for (const c of str) {
if (c === char) {
count++;
}
}
return count;
}

const inputString = "hello";


const inputChar = "l";
const occurrences = countOccurrences(inputString, inputChar);
console.log(occurrences); // Output: 2

Find the Largest Number in an Array


function findLargestNumber(arr) {

return Math.max(...arr);

const numbers = [3, 8, 2, 10, 5];

const largestNumber = findLargestNumber(numbers);

console.log(largestNumber); // Output: 10

Find the Longest Word

function findLongestWord(sentence) {

const words = sentence.split(' ');

let longestWord = "";

for (const word of words) {

if (word.length > longestWord.length) {

longestWord = word;
}

return longestWord;

const inputSentence = "The quick brown fox jumps over the lazy dog";

const longestWord = findLongestWord(inputSentence);

console.log(longestWord); // Output: "jumps"

Find the Missing Number

function findMissingNumber(arr) {
const n = arr.length + 1;
const expectedSum = (n * (n + 1)) / 2;
const actualSum = arr.reduce((sum, num) => sum + num, 0);
return expectedSum - actualSum;
}

const numbers = [1, 2, 4, 5, 6];


const missingNumber = findMissingNumber(numbers);
console.log(missingNumber); // Output: 3

FizzBuzz

for (let i = 1; i <= 100; i++) {


if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}

Remove Duplicates from an Array

function removeDuplicates(arr) {
return [...new Set(arr)];
}

const numbers = [1, 2, 2, 3, 4, 4, 5];


const uniqueNumbers = removeDuplicates(numbers);
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5]

Reverse a String

function reverseString(str) {
return str.split('').reverse().join('');
}

const inputString = "Hello, World!";


const reversedString = reverseString(inputString);
console.log(reversedString); // Output: "!dlroW ,olleH"

evenOdd

// program to check if the number is even or odd


// take input from the user
const number = prompt("Enter a number: ");

//check if the number is even


if(number % 2 == 0) {
console.log("The number is even.");
}

// if the number is odd


else {
console.log("The number is odd.");
}

Factorial
function factorial(n){
let answer = 1;
if (n == 0 || n == 1){
return answer;
}
else if(n > 1){
for(var i = n; i >= 1; i--){
answer = answer * i;
}
return answer;
}
else{
return "number has to be positive."
}
}
let n = 4;
answer = factorial(n)
console.log("Factorial of " + n + " : " + answer);
program to generate fibonacci series up to n terms

// take input from the user


const number = parseInt(prompt('Enter the number of terms: '));
let n1 = 0, n2 = 1, nextTerm;

console.log('Fibonacci Series:');

for (let i = 1; i <= number; i++) {


console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}

find the min/max element of an Array

function findMinMax() {
let Arr = [50, 60, 20, 10, 40];

let minValue = Math.min(...Arr);


let maxValue = Math.max(...Arr);

console.log("Minimum element is:" + minValue);


console.log("Maximum Element is:" + maxValue);
}

findMinMax()

You might also like