DEV Community

Neetish Kumar
Neetish Kumar

Posted on • Updated on

JS Easy Programming Problem

Q1. Write a JavaScript program to determine given string is a palindrome or not.

function isPalindrome(string) {

// Remove non-alphanumeric characters and convert to lowercase
const alphaNumericString = string.toLowerCase().replace(/[\W_]/g, '');

// Reverse the alpha Numeric String
const reversedString = alphaNumericString.split('').reverse().join('');

// Check if the alpha Numeric string is equal to its reverse
return alphaNumericString === reversedString;
}

// Test the function
const inputString = 'Never odd or even.';

if(isPalindrome(inputString)){
  console.log('Given string is a palindrome.');
} else {
  console.log('Given string is a palindrome.');
}

Enter fullscreen mode Exit fullscreen mode

Q2. Write a JavaScript Program to Reverse a Given String Without Using Built-in Methods

function reverseString(str) {
    let length = str.length;
    let reverseStr = "";
    for (let i = length - 1; i >= 0; i--) {
        reverseStr += str[i];
    }
    return reverseStr;
}
console.log(reverseString("hello")); // olleh
Enter fullscreen mode Exit fullscreen mode

Q3. Write a function that capitalizes the first letter of each word in a given string.

function capitalizeFirstChar(string) {

  let wordsOfString = string.split(' ');
  for (let i = 0; i < wordsOfString.length; i++) {
  wordsOfString[i] = wordsOfString[i].charAt(0).toUpperCase() + 
  wordsOfString[i].slice(1);
}
    return wordsOfString.join(' ');
}
console.log(capitalizeFirstChar("Test this code")); 
// Output: "Test This Code"

Enter fullscreen mode Exit fullscreen mode

Q4. Write a JavaScript program that sums the elements of in given array.

function sumOfGivenArray(arr){
    let sum = 0;
    for (let i = 0; i < arr.length; i++){
        sum += arr[i]
    }
    console.log(sum);
}
sumOfGivenArray([1,2,3,4,5,6,7,8,9,10]) // 55
Enter fullscreen mode Exit fullscreen mode

Q5. Write a JavaScript program that remove all the duplicate elements of in given array.

function removeDuplicateArray(arr){
    const arrayWithoutDuplicaition = [];
    for(let i = 0; i < arr.length; i++){
        if(!arrayWithoutDuplicaition.includes(arr[i])) {
            arrayWithoutDuplicaition.push(arr[i])
        }
    }
    console.log(arrayWithoutDuplicaition);
}
removeDuplicateArray([1,2,3,3.5, 4.5, 4, 4, 4, 3, 9, 99, 999]);
Enter fullscreen mode Exit fullscreen mode

Q6. Write a JavaScript program that returns the largest number from an array of numbers.

function findLargestNumber(arr){
    let largestNumber = 0;
    for(let i = 0; i < arr.length; i++){
        if(largestNumber < arr[i]) {
            largestNumber = arr[i];
        }
    }
    console.log(largestNumber);
}
findLargestNumber([1,2,3,3.5, 4.5, 4, 4, 4, 3, 9, 99, 99, 99.5]); // 99.5

Enter fullscreen mode Exit fullscreen mode

Top comments (0)