Problem list:
1. Reversing a String
Problem: Write a function that reverses a given string.
function reverseString(str) {
return str.split('').reverse().join('');
}
// Example usage
console.log(reverseString('hello')); // Output: 'olleh'
Way 2:
const reverseString = str => {
return str.split("").reverse().join("");
}
console.log(reverseString("rakib")); // Output: 'bikar'
Way 3:
const reverseString = str => str.split("").reverse().join("");
console.log(reverseString("rakib")); // Output: 'bikar'
2. Finding the Largest Number in an Array
Problem: Write a function that finds the largest number in an array.
function findLargestNumber(arr) {
return Math.max(...arr);
}
// Example usage
console.log(findLargestNumber([3, 9, 1, 25, 7])); // Output: 25
findLargestNumber = arr => Math.max(...arr)
console.log(findLargestNumber([3,4,5,6,69]))
3. Checking for Palindromes
Problem: Create a function that checks whether a string is a palindrome.
Way 1:
function isPalindrome (str) {
const reverse = str.split('').reverse().join("");
return str === reverse
}
console.log(isPalindrome("rakib"))
Way 02:
isPalindrome = str => {
const reverse = str.split('').reverse().join("")
return str === reverse
}
console.log(isPalindrome("lol"))
// Output: false
4. Counting the Number of Words in a Sentence
Problem: Write a function that counts the number of words in a given sentence.
function count (sentence) {
const words = sentence.split("").filter(word => word.trim() !== "")
return words.length
}
console.log(count("i love you"))
const count = sentence => {
const words = sentence.split("").filter(word=> word.trim() !=="")
return words.length
}
console.log(count("i love you"))