JS Easy Programming Problem

neetish_kumar_db8a8a2e8bb

Neetish Kumar

Posted on September 13, 2024

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

Q7. Write a JavaScript program to print the Fibonacci sequence.

function fibonacciSequence(n) {
  let series = [];

  for (let i = 0; i < n; i++) {
    if (i === 0) {
      series.push(0);
    } else if (i === 1) {
      series.push(1);
    } else {
      series.push(series[i - 1] + series[i - 2]);
    }
  }

  return series;
}

const number = 10;
const result = fibonacciSequence(number);
console.log(result); // [ 0, 1,  1,  2,  3, 5, 8, 13, 21, 34]

Enter fullscreen mode Exit fullscreen mode

Q8. Write a function that takes a deeply nested array as input and returns a new array that contains all the elements of the original array, flattened to a single level.

function flatenArr(arr){
  let flatArr = [];

  arr.forEach((item) => {
    if(Array.isArray(item)) {
      flatArr = flatArr.concat(flatenArr(item));
    } else {
      flatArr.push(item)
    }
  })
  return flatArr;
}

const nestedArr = [1, [2, [3, 4], 5], [6, [7, 8]]];
const result = flatenArr(nestedArr);
console.log(result)
Enter fullscreen mode Exit fullscreen mode

Q9. Write a JavaScript function that takes an array of strings as an argument and returns an object representing the count of each string in the array.

function countOcc(arr) {
  let count = {};
  for(let i = 0; i < arr.length; i++){
    let item = arr[i];
    if(!count[item]) {
      count[item] = 0;
    }
    count[item]++
  }
  return count;
}
const arr = ['banana', 'apple', 'banana', 'apple', 'apple'];
const result = countOcc(arr)
console.log(result); // output { banana: 2, apple: 3 }
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
neetish_kumar_db8a8a2e8bb
Neetish Kumar

Posted on September 13, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related