Skip to main content

Common

Convert a string to title case (capitalize the first letter of each word). toTitleCase

function toTitleCase(str: string): string { 
return str.replace(/\b\w/g, l => l.toUpperCase());
}

A function to check if a given string is a palindrome.

function isPalindrome(str: string): boolean {
// Remove punctuation and whitespace and convert to lowercase
const cleanedStr = str.replace(/[^\w]/g, '').toLowerCase();
// Compare the cleaned string with its reverse
return cleanedStr === cleanedStr.split('').reverse().join('');
}
// Example usage:
const testString1 = "A man, a plan, a canal, Panama!";
console.log(isPalindrome(testString1)); // Output: true

const testString2 = "racecar";
console.log(isPalindrome(testString2)); // Output: true

const testString3 = "hello";
console.log(isPalindrome(testString3)); // Output: false

Implement a function to reverse a string without using the built-in reverse() method.

function reverseString(str: string): string {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}

console.log("reverseString: ", reverseString('hello world'));

Given an array of numbers, write a function to find the largest and smallest numbers in the array.

function findMinMax(arr) { 
let min = Math.min(...arr);
let max = Math.max(...arr);
return [min, max];

}

console.log(findMinMax([1, 2, 3]));

...to be continue

Reference

https://the-algorithms.com/category/datastructures https://www.keka.com/javascript-coding-interview-questions-and-answers